开发者

Iphone text file reading

开发者 https://www.devze.com 2023-03-18 20:15 出处:网络
I\'m trying to read data from 1.txt in my iphone project\'s resources folder using the following code:

I'm trying to read data from 1.txt in my iphone project's resources folder using the following code:

NSString *fileContent = [NSString stringWithContentsOfFile:[[NSString stringWithFormat:@"%d", currentLevel] stringByAppendingString:@".txt"] encoding:NSUTF8StringEncoding error:nil]; 
NSAssert(fileContent!=nil, @"file's contents are nil");

currentLevel is an integer which is initially set to 1. It doesn't matter whether I replace [[NSString stringWithFormat:@"%d", currentLevel] stringByAppendingString:@".txt"] with @"1.txt" (for testing th开发者_C百科e first level) because both times NSAssert fails

why isn't this working? I've imported 1.txt to xcode and it's in the resources file. it's not empty.


Try this:

NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d", currentLevel] ofType:@"txt"];
NSString *fileContents = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:NULL];
0

精彩评论

暂无评论...
验证码 换一张
取 消