Q:

Convert dispatch_data_t to NSString after a read operation

In my small iOS app I have a CSV file that represents an histogram. The content of file is:
label0,value0
...
labelN,valueN
In my ViewController I read the file async with Grand Central Dispatch this way:
//create the channel with which read the CSV file
dispatch_io_t ch = dispatch_io_create_with_path(DISPATCH_IO_STREAM, [[[NSBundle mainBundle] pathForResource:@"histogram1" ofType:@"csv"] UTF8String], O_RDONLY, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(int error) {

    if(!error){
        NSLog(@"Channel created!");
    }

});

//read the whole file
dispatch_io_read(ch, 0, SIZE_MAX, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(bool done, dispatch_data_t dataRead, int error) {

    if(!error && done){
        NSString *ma = ; //???
        NSLog(@"%@", ma);
    }

});

I'd like to convert the dataRead (of type dispatch_data_t) to NSString to extract the values that represent the bars of the histogram. 

A:

There are a number of ways to do this, but the function you're looking for is dispatch_data_apply. Assuming the string you're reading is UTF8, this should work:
@interface NSString (FromDispatchData)

+ (instancetype)stringFromDispatchData: (dispatch_data_t)data;

@end

@implementation NSString (FromDispatchData)

+ (instancetype)stringFromDispatchData: (dispatch_data_t)data
{
    if (!data)
        return nil;

    NSMutableString* str = [NSMutableString string];

    dispatch_data_apply(data, ^bool(dispatch_data_t region, size_t offset, const void *buffer, size_t size) {
        [str appendString: [[NSString alloc] initWithBytes:buffer length:size encoding: NSUTF8StringEncoding]];
        return true;
    });

    return [[self class] stringWithString: str];
}

@end

