开发者

Iphone text file reading

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜