Here's a simple category on NSString to parse a CSV string that has commas embedded inside quote blocks.
#import "NSString+CSV.h"
@implementation NSString (CSV)
- (NSArray *)componentsSeparatedByComma
{
BOOL insideQuote = NO;
NSMutableArray *results = [[NSMutableArray alloc] init];
NSMutableArray *tmp = [[NSMutableArray alloc] init];
for (NSString *s in [self componentsSeparatedByString:@","]) {
if ([s rangeOfString:@"\""].location == NSNotFound) {
if (insideQuote) {
[tmp addObject:s];
} else {
[results addObject:s];
}
} else {
if (insideQuote) {
insideQuote = NO;
[tmp addObject:s];
[results addObject:[tmp componentsJoinedByString:@","]];
tmp = nil;
tmp = [[NSMutableArray alloc] init];
} else {
insideQuote = YES;
[tmp addObject:s];
}
}
}
return results;
}
@end
This assumes you've read your CSV file into an array already:
myArray = [myData componentsSeparatedByString:@"\n"];
The code doesn't account for escaped quotes, but it could easily be extended to.