NSBundle pathForResource 为 NULL
我正在使用 xcode 和 objc 创建一个简单的应用程序,我需要从文件加载 NSDictionary,但我无法使用 NSBundle 获取文件的路径:
NSString *l = [[NSBundle mainBundle] pathForResource:@"LoginStatuses" ofType:@"plist"];
NSLog(@"%@", l);
当我运行此代码时,我得到:
2010-10-16 10:42:42.42 Sample[5226:a0f] (null)
我不知道为什么。
我创建了一个名为 Resources 的组,并在其中添加了 LogingStatuses.plist:
I'm creating a simple application with xcode and objc and I need to load an NSDictionary from a file, but I can't get the path to the file using NSBundle:
NSString *l = [[NSBundle mainBundle] pathForResource:@"LoginStatuses" ofType:@"plist"];
NSLog(@"%@", l);
When I run this code I get this:
2010-10-16 10:42:42.42 Sample[5226:a0f] (null)
And I don't know why.
I created a group called Resources and there I added the LogingStatuses.plist:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
因此,在我获得源代码后,这是这个问题的解决方案:
我并没有真正注意发布的屏幕截图,但目标是“命令行工具”类型...并且由于这些没有包
[NSBundle mainBundle]
当然返回 nil。 Xcode 并没有抱怨它无法执行“复制捆绑资源”步骤,而是默默地跳过它,这是相当具有误导性的。解决方案只是添加一个类型为“Application”的新目标,以便生成基于捆绑包的应用程序。然后选中此新目标的所有源和资源的目标成员资格复选框。然后 plist 路径就被正确解析了。
So here's the solution for this problem after I got the source:
I didn't really pay attention to the posted screenshot, but the target is of type "Command-line Tool"... and since those don't have a bundle
[NSBundle mainBundle]
of course returns nil. It's pretty misleading that Xcode doesn't complain that it can't execute the "Copy Bundle Resources" step, it just silently skips it.Solution is simply to add a new target, of type "Application" so a bundle-based application is generated. Then check the Target Membership checkboxes for all sources and resources for this new target. The plist paths are correctly resolved then.
我试图让我的 iPhone 应用程序使用默认的 sqlite 数据库,但该死的应用程序找不到它。结果我必须确保 .sqlite 文件位于捆绑资源中。
现在你的应用程序将找到这个默认的 sqlite 数据库。
I was trying to get my iPhone app to use a default sqlite database and the darn app couldn't find it. Turned out that I had to make sure that the .sqlite file was in the bundle resource.
now your app will find this default sqlite database.
该文件是否确实包含在目标中(因此将被复制到捆绑包中)?有两种方法可以查找/设置:
第一种方法:右键单击(或 Cmd 单击)文件,选择“获取信息”。然后单击“目标”选项卡并确保检查文件中是否有所需的目标。
第二种方法:在项目浏览器中的文件浏览器标题上右键单击(或 Cmd-clock)(它可能会显示“组和文件”)。然后选择“目标会员”。现在,每个可以成为目标成员的文件(例如 .m 文件或资源)旁边都有复选框。再次确保选中文件旁边的复选框。
Is the file really included in the target (and will therefor be copied to the bundle) ? There two ways to find out/set that:
First way: right-click (or Cmd-click) on the file, select "Get Info". Then click on the "Targets" tab and make sure the file is checked for the desired target(s).
Second way: right-click (or Cmd-clock) in the project browser on the header of the file browser (it will likely read "Groups & Files"). Then select "Target Membership". Now you have checkboxes next to each file that can be member of a target (like .m files or resources). Again, make sure the checkbox next to your file is checked.
由于我在这里用谷歌搜索,没有找到答案,但后来自己发现了它,我将其留在这里......
我有2个文件:tray.png和[电子邮件受保护] 适用于 Retina。这些文件已自动添加到“复制捆绑资源”中。
但是:
没有返回实际复制到捆绑包中的文件!原因是:IDE 创建了一个 TIFF 文件 tray.tiff(联合 tray.png 和 [电子邮件受保护]),所以
... ofType:@"tiff"]
有帮助!Since I have googled here, did not find the answer, but then discovered it by myself, I'll leave it here...
I had 2 files: tray.png and [email protected] for Retina. The files were added to "Copy Bundle Resources" automatically.
But:
did not return the file actually copied to the bundle! The reason was: IDE created one TIFF file tray.tiff (joint tray.png and [email protected]), so
... ofType:@"tiff"]
helped!我的问题和解决方案与德米特里·伊萨耶夫的非常相似。我试图获取 xib 文件的路径。两个调用(在 Swift 中) pathForResource("myfile", ofType: "xib") 和 pathForResource("myfile.xib", ofType: nil) 均失败。
解决方案是使用扩展名“nib”代替:
My problem and solution are very similar to Dmitriy Isaev's ones. I have tried to get path for a xib file. Both calls (in Swift) pathForResource("myfile", ofType: "xib") and pathForResource("myfile.xib", ofType: nil) are failed.
The solution is to use extension "nib" instead:
我今天在命令行项目中遇到了这个问题。
幸运的是,解决方案很简单。只需转到“构建阶段”,单击“+”并添加新的“复制文件”阶段。选择“资源”作为目标,然后添加您要使用的任何文件。
现在您仍然可以使用
[NSBundle mainBundle]
并且它应该可以工作!I encountered this issue today with a Command Line project.
Luckily, the solution is easy. Simply go to "Build Phases", click on "+" and add a new "Copy Files" phase. Choose "Resources" as Destination, and add any files you want to use.
Now you can still use
[NSBundle mainBundle]
and it should work!在我的例子中(执行 XCTestCase),由于某种原因,资源存储在非主 Bundle 中。我通过首先检查哪个捆绑测试类属于来解决这个问题:
希望这也可以帮助其他人。
In my case (executing XCTestCase) for some reason resources were stored in non-main Bundle. I fixed the problem by checking first which bundle test class belongs to:
Hopefully this can help someone else as well.
iPad 上的文件名区分大小写。您需要使用小写字母。
Filename is case sensitive on iPad. You need use small letters.
有一种方法可以为命令行应用程序执行此操作。
使用“复制文件”而不是使用“复制捆绑资源”。对于“目的地”,选择“资源”。这会将文件复制到应用程序包中,并且 Bundle.main 可以找到它。
There is a way to do this for a Command-Line app.
Instead of using "Copy Bundle Resources" use "Copy Files". For "Destination" select "Resources". This will copy the file to the app bundle and the Bundle.main can find it.
确保资源的文件名拼写正确。我刚刚通过艰难的方式才了解到这一点。 :)
Make sure you spell your resource's file name properly. I just learned that the hard way. :)