我目前正在尝试使用CHCSVParser解析包含1500多个条目和8行的CSV文件。我已成功地解析了文件,得到了一个NSStrings的NSArray数组。
例如,这是我得到的内容:
例如,这是我得到的内容:
Loading CSV from: (
(
Last,
First,
Middle,
Nickname,
Gender,
City,
Age,
Email
),
(
Doe,
John,
Awesome,
"JD",
M,
"San Francisco",
"20",
"john@john.doe"
),
我该如何将这个内容分类为一个Person对象,并使用NSPredicate进行过滤,就像Mattt Thompson在这里所做的那样。
以下是我初始化解析器的方法:
//Prepare Roster
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:@"myFile" ofType: @"csv"];
NSArray *myFile = [NSArray arrayWithContentsOfCSVFile:pathToFile options:CHCSVParserOptionsSanitizesFields];
NSLog(@"Loading CSV from: %@", myFile);
以下是我希望将我的代码与链接文章中的Mattt所做的相同操作:
NSArray *firstNames = @[ @"Alice", @"Bob", @"Charlie", @"Quentin" ];
NSArray *lastNames = @[ @"Smith", @"Jones", @"Smith", @"Alberts" ];
NSArray *ages = @[ @24, @27, @33, @31 ];
NSMutableArray *people = [NSMutableArray array];
[firstNames enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *person = [[Person alloc] init];
person.firstName = firstNames[idx];
person.lastName = lastNames[idx];
person.age = ages[idx];
[people addObject:person];
}];