4

我有一个文本文件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"}

我错过了什么吗?

4

1 回答 1

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);

希望不会出错!

于 2013-06-09T12:09:06.753 回答