iOS 6中的文件路径给我null

use*_*381 0 iphone nsfilemanager ipad ios

我正在使用iPhone6模拟器,我正在尝试使用以下任何代码获取文件(任何扩展名为pdf的html):

 NSString *file = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@.pdf", documentName] ofType:nil];
Run Code Online (Sandbox Code Playgroud)

要么

NSString *file2 = [[NSBundle mainBundle] pathForResource:documentName ofType:@"pdf"];
Run Code Online (Sandbox Code Playgroud)

我确定该文件存在于Resources文件夹中并且我没有添加该文件,我以编程方式从Web下载并在此路径中查看(/ Users/myMac/Library/Application Support/iPhone Simulator/6.9/Applications /E60F22DD-7301-48EF-AB25-B9D42FA6AD49/myApp.app)我看到了文件,但是代码看不到它

但是这个代码有时会让我为null,而有些时候会给我这个文件.

为什么会发生这种情况以及如何防止这种情况并使其始终为我提供文件.考虑我使用

if(file != nil)
Run Code Online (Sandbox Code Playgroud)

并希望如果file == nil尝试使用任何方式打开它.

提前致谢.

Tom*_*cki 5

确保您的文件已添加到项目的目标中.选择目标,然后选择Build Phases-> Copy Bundle Resources.该文件应该在那里.

有时值得清理您的项目并从头开始构建.我已经看到添加文件没有复制到模拟器/设备直到执行清理的情况.