NSBundle pathForResource:返回nil

Hen*_*old 2 file-io objective-c nsstring ios

我试图使用以下代码从iOS上的Xcode中的支持文件文件夹中访问.txt文件:

NSString* filePath = @"filename.txt";

NSLog(@"File path: %@", filePath);

NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];

NSLog(@"File root: %@", fileRoot);
Run Code Online (Sandbox Code Playgroud)

第一个NSLog打印出我希望它打印的内容,但最后一个NSLog总是打印

文件根:(null)

在(尝试)将文件读入内存后从文件中访问文本也简单地给了我一个(null)打印输出.

这个问题的解决方案可能就在我的鼻子底下,我似乎无法找到它.任何帮助非常感谢!提前致谢.

Mar*_*n R 18

filePath已包含后缀".txt",因此在查找资源时不得再次指定它.或

NSString* filePath = @"filename.txt";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:nil];
Run Code Online (Sandbox Code Playgroud)

要么

NSString* filePath = @"filename";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];
Run Code Online (Sandbox Code Playgroud)