我正在尝试将 csv 文件中的一组默认数据加载到我的核心数据数据库中。所以最初我试图读取一个 csv 文件并将其输出到日志中,然后再尝试将其添加到核心数据数据库中。
这是我正在使用的代码;
//Find import file;
NSString *defaultCSVPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"csv"];
//Get the data into a string
NSString *fileString = [NSString stringWithContentsOfFile:defaultCSVPath encoding:NSUTF8StringEncoding error:nil];
if ( nil == fileString ) {
NSLog(@"Could not open file data.csv");
abort();
}
// Create the scanner
NSScanner *scanner = [NSScanner scannerWithString:fileString];
// Ignore new lines
[scanner setCharactersToBeSkipped:
[NSCharacterSet characterSetWithCharactersInString:@"\n"]];
NSString *sNumber = @"";
NSString *sTitle = @"";
//Gets to here and this expression is never TRUE
while ( [scanner scanString:@"," intoString:&sNumber] && [scanner scanString:@"," intoString:&sTitle]) {
NSLog(@"sNumber:%@ sTitle:%@",sNumber,sTitle);
}
我使用的样本数据是;
A15Q,Test1
F74443AAZ,Test2
当我跟踪代码时,我到达了 while 子句,它只是跳过了它。