NSBundle pathForResource: returns nil



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

NSString* filePath = @"filename.txt";
NSLog(@"File path: %@", filePath);
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:@"txt"];
NSLog(@"File root: %@", fileRoot);

第一个 NSLog 打印的只是我希望它打印的内容,但最后一个 NSLog 总是简单地打印

文件根目录:(空)

在(尝试)将

文件读入内存后访问文件中的文本也只是给了我一个(空)打印输出。

这个问题的解决方案可能就在我眼皮底下,我似乎找不到它。任何帮助都非常感谢!提前谢谢。

filePath已经包含后缀".txt",因此您不得指定它再次定位资源时。也

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

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

最新更新