Clip
Clip

Reputation: 3078

Using .csv file to create objects Objective-C

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?

enter image description here

Upvotes: 0

Views: 89

Answers (1)

lucianomarisi
lucianomarisi

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

Related Questions