8

I am trying to load a .csv file to Xcode using Objective-C and then I want to create two different arrays. The first array should have values from the first 2 columns and the second array the values of the third column.

I know that what I am looking for is fairly similar to this question, but I am completely newbie in Objective-C and I am a bit confused.

Until now I have tried writing the following code:

NSString* fileContents = [NSString stringWithContentsOfURL:@"2014-07-16_15_41_20.csv"];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (int i = 0; i < rows.count; i ++){
     NSString* row = [rows objectAtIndex:i];
     NSArray* columns = [row componentsSeparatedByString:@","];
}

So, is this piece of code correct until now? Also, how can I divide columns into 2 different arrays in the way I described above?

Community
  • 1
  • 1
Galil
  • 859
  • 2
  • 16
  • 40
  • 1
    Be careful when parsing CSV if you don't control its source. There are lots of weird exceptions that can show up, as CSV isn't a well-defined format. Make sure you know you're never going to have elements which can contain commas (backslash escaped? enclosed in quotes?) if you're just going to split on commas like you're doing here. – ahruss Jul 24 '14 at 19:23

1 Answers1

13

Your code seems correct. But it's better to use Cocoa Fast Enumeration instead of a for loop with integers.

To divide into arrays your code could look like this.

NSMutableArray *colA = [NSMutableArray array];
NSMutableArray *colB = [NSMutableArray array];
NSString* fileContents = [NSString stringWithContentsOfURL:@"2014-07-16_15_41_20.csv"];
NSArray* rows = [fileContents componentsSeparatedByString:@"\n"];
for (NSString *row in rows){
     NSArray* columns = [row componentsSeparatedByString:@","];
     [colA addObject:columns[0]];
     [colB addObject:columns[1]];
}

Read more about NSMutableArray

idmean
  • 14,540
  • 9
  • 54
  • 83
  • I tried to check the results taken using `NSLog(@"ColumnA: %@",colA);` but I get some errors like: `[__NSCFConstantString isFileURL]: unrecognized selector sent to instance 0x4a90 2014-07-24 20:30:06.820 MyLocationMap[2759:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString isFileURL]: unrecognized selector sent to instance 0x4a90'` – Galil Jul 24 '14 at 19:32
  • You should better use NSString's `stringWithContentsOfFile:encoding:error:`. Than you will have to pass a valid path. Where does this CSV file resides? – idmean Jul 24 '14 at 19:36
  • I dragged and dropped the CSV file in Xcode directory where all source code files exist. Should I give the full path of the directory where the CSV file exists? – Galil Jul 24 '14 at 19:42
  • Ahh. You added the CSV file to your app, didn't you? You will have to use `[[NSBundle mainBundle] pathForResource:@"2014-07-16_15_41_20" ofType:@"csv"]` to get the path. Because the file is moved into the created application file as a resource. – idmean Jul 24 '14 at 19:43