Reputation: 3078
I am mapping my university and they provide a .csv file with a list of all of the buildings on campus. Which includes information like Building #, Name, Code, Street address, Street coordinate ect.
I want to read the values from this file to create a NSArray
of Building
objects.
This is how the file is formatted, how would I go about doing this?
Upvotes: 0
Views: 89
Reputation: 1552
Load the csv into a NSString
.
NSString *path = [[NSBundle mainBundle] pathForResource:@"filename"
ofType:@"csv"];
NSStringEncoding encoding = 0;
NSError *error = nil;
NSString *csvString = [NSString stringWithContentsOfFile:path
usedEncoding:&encoding
error:&error];
Parse it with your preferred method, I use an open source library, CHCSVParser
https://github.com/davedelong/CHCSVParser
NSArray *fields = [csvString CSVComponents]; // you need to import "CHCSVParser.h"
Upvotes: 1