简体   繁体   English

在XCode中读取CSV文件

[英]Read csv file in xcode

I have this code to read a csv file 我有这段代码来读取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);
}

but my file csv don't have a path but it's in xcode project folder...what can I solve? 但是我的文件csv没有路径,但是在xcode项目文件夹中...我该怎么解决?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM