Best way to get closer to loading a class from a comma-delimited file

I am new to objective-c and object-oriented programming in general and have a theoretical, stylistic type of question. I want to load a class table with entries from a comma-delimited file. The data in the file consists of many records consisting of a short key, followed by several string values, all limited to commas.

There are a million ways to do this, but I ask what would be better than it would be from a theoretical point of view. I would like to stay away from any kind of XML coding for now, but I will probably end up moving to this format as soon as I get the writer together.

I could use the function to get the “next record” and pass the structure to and from the function, create a new instance of the class, load it from the structure and then add it to the array. I would use the stringWithContentsOfFile method to first load the file into a string, then use string functions and some pointers to go through the file to return the elements of the structure that 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 that may sound more theoretical that will work, at least as well?

+3
source share
1 answer

CSV, ? .

:

#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
    }
}
+4

Source: https://habr.com/ru/post/1780142/


All Articles