I am a fairly new to objective-c and to object oriented programming in general and have a theoretical, stylistic type question. What I want to do is load a table of classes with entries from a comma delimited file. The data in the file consists many entries made up of a short key followed by several string values, all delimited by commmas.
There are a million ways to do this but I'm asking what would be the way which would be best from a strictly theoretical point of view. I'd like to stay away from any sort of XML coding for the moment but will probably eventually convert to that format once I get an entry program together.
I could use a function to get the 'next record' and pass a structure in and out of the function, create a new instance of the class, load it up from the structure, then add it into an array. I'd use the stringWithContentsOfFile method to load the开发者_开发知识库 file into a string initially then use string functions and some pointers to march through the file to return the structure elements which I would then load into the class.
Does this seem like a reasonable way to do this in objective-c or is there a better method which is perhaps more theoretically sound which would work at least as well?
You have a CSV file, and you want to read it? There's some code for that.
The simplest approach would be something like:
#import "CHCSV.h"
NSString * csvFile = ...; //path to the CSV file
NSError * error = nil;
NSArray * contents = [NSArray arrayWithContentsOfCSVFile:csvFile
encoding:NSUTF8StringEncoding
error:&error];
if (contents == nil) {
NSLog (@"Error %@", error);
} else {
for (NSArray * row in contents) {
NSLog(@"CSV fields in this line: %@", row);
// "row" contains all the fields (as NSStrings) that were present
// on this line of CSV
}
}
精彩评论