1

I am retriving some file from the web containing data in a specific format which I would like to parse. However I know only how to get the file from the web:

dispatch_async(server_queue, ^{
    NSData* data = [NSData dataWithContentsOfURL:
                    kURL];
    [self performSelectorOnMainThread:@selector(parseData:)
                           withObject:data waitUntilDone:YES];
});

In the parse method I would like to tokenize each line of the file but I am not sure how to extract the lines from the NSData object.

-(void)parseData:(NSData *)responseData {
    //tokenize each line of responseData
}

Any suggestion?

mm24
  • 9,280
  • 12
  • 75
  • 170
  • What are you fetching exactly? – Larme May 12 '14 at 11:17
  • You can convert the NSData to an NSString. Then split that string at every linebreak and loop over the resulting array. – Marc May 12 '14 at 11:19
  • Convert the NSData to NSString and use CFStringTokenizer. Apple link [here](https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFStringTokenizerRef/Reference/reference.html#//apple_ref/doc/uid/TP40005095-CH203-SW1) or see [StackOverflow Answer](http://stackoverflow.com/questions/6877659/how-to-get-an-array-of-sentences-using-cfstringtokenizer) – Pancho May 12 '14 at 11:28

1 Answers1

1

NSData is not in a format where you can go through to parse it. Just convert it to a NSString like this:

-(void)parseData:(NSData *)responseData
{
    NSString *stringFromData = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSArray *eachLineOfString = [stringFromData componentsSeparatedByString:@"\n"];
    for (NSString *line in eachLineOfString) {
        // DO SOMETHING WITH THIS LINE
    }
}
Ben
  • 3,455
  • 3
  • 26
  • 31