cyclingIsBetter
cyclingIsBetter

Reputation: 17591

Read csv file in xcode

I have this code to read a csv file

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?

Upvotes: 0

Views: 2728

Answers (1)

pythonquick
pythonquick

Reputation: 10869

You can use the NSBundle pathForResource:ofType: method, for a given file name (resourceFileName):

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

Upvotes: 1

Related Questions