我没有使用stringWithContentsOfFile找到一个txt文件

And*_*rew 4 iphone xcode objective-c ios

我有一个文本文件thetext.txt.这是在我的项目中,并在构建设置中复制.就像我的GL着色器和纹理一样(工作正常.)

NSError *errorReading;
NSArray *linesOfText = [[NSString stringWithContentsOfFile:@"thetext.txt"
                                       encoding:NSUTF8StringEncoding
                                          error:&errorReading]
             componentsSeparatedByString:@"\n"];

NSLog(@"Reading error  %@",errorReading); 
Run Code Online (Sandbox Code Playgroud)

它将以下内容打印到控制台.

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"}
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

Gro*_*oot 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);
Run Code Online (Sandbox Code Playgroud)

希望没有错误!