我有一个文本文件thetext.txt
。这是在我的项目中,并在构建设置中复制。就像我的GL着色器和纹理一样(工作正常。)
NSError *errorReading;
NSArray *linesOfText = [[NSString stringWithContentsOfFile:@"thetext.txt"
encoding:NSUTF8StringEncoding
error:&errorReading]
componentsSeparatedByString:@"\n"];
NSLog(@"Reading error %@",errorReading);
它将以下内容输出到控制台。
Reading error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x896fff0 {NSFilePath=thetext.txt, NSUnderlyingError=0x896ff60 "The operation couldn’t be completed. No such file or directory"}
我遗失了什么吗?
答案 0 :(得分:17)
这会失败,因为您传递的是文件名而不是文件的路径。试试这样的事情
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"thetext" ofType:@"txt"];
NSError *errorReading;
NSArray *linesOfText = [[NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&errorReading]
componentsSeparatedByString:@"\n"];
NSLog(@"Reading error %@",errorReading);
希望没有错误!