在xcode中读取csv文件

时间:2011-04-01 10:55:30

标签: objective-c xcode csv path

我有这段代码来读取csv文件

NSString *fileString = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:outError];
if (!fileString) {
NSLog(@"Error reading file.");
}
NSScanner *scanner = [NSScanner scannerWithString:fileString];
[scanner setCharactersToBeSkipped:[NSCharacterSetcharacterSetWithCharactersInString:@"\n#; "]];

NSString *id = nil, *hashOne = nil, *hashTwo = nil, *hashThree = nil, *hashFour = nil;
while ([scanner scanUpToString:@"#" intoString:&id] && [scanner scanUpToString:@"# intoString:&hashOne] && [scanner scanUpToString:@"#" intoString:&hashTwo] && [scanner scanUpToString:@"#" intoString:&hashThree] && [scanner scanUpToString:@";" intoString:&hashFour]) {
 // Process the values as needed.
 NSLog(@"id:%@, 1:%@, 2:%@, 3:%@:, 4:%@", id, hashOne, hashTwo, hashThree, hashFour);
}

但我的文件csv没有路径,但它在xcode项目文件夹中...我能解决什么?

1 个答案:

答案 0 :(得分:1)

对于给定的文件名(resourceFileName),您可以使用NSBundle pathForResource:ofType:方法:

pathToFile = [NSBundle mainBundle] pathForResource: resourceFileName ofType: @"csv"];