使用 [[NSBundle mainBundle] pathForResource 获取 .plist 路径时出现问题
我是一个 Objective C 菜鸟,我不知道足以解释以下问题。
此代码有效:
NSString *plistPath = @"/Users/andrewf/MyApp/Resources/Plates.plist";
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
我的字典对象已按预期加载了值。
此代码不起作用:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Plates" ofType:@"plist"];
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
plistPath 返回的值为 nil。无论我是否在调用中包含 inDirectory:@"Resources" ,情况都是如此。当尝试打开资源目录中的 .plist 文件时,我发现的所有示例都不包含 inDirectory 。
我已经确认该文件存在于正确的位置,甚至重新创建了它以确保确定。
这看起来是一个很简单的问题,但我很困惑。请帮忙。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您对
pathForResource:
在哪里寻找感到困惑。这是可行的:
请注意,该路径并不指向您的应用程序。它指向您的项目目录。您的 plist 应该位于
@/Users/andrewf/MyApp/build/Release/MyApp.app/Resources/Plist.plist"
(对于 iPhone 应用程序,这会有所不同,更像@"/Users/andrewf/Library/Application Support/iPhone Simulator/User/Applicatons/(unique id)/MyApp.app/Resources/Plates.plist"
),它位于 Resources 文件夹内 pathForResource: 正在查找的应用程序这意味着您的资源不处于构建目标的“复制捆绑资源”阶段。组和文件区域位于该阶段内。
I think you're confused as to where
pathForResource:
is looking.This works:
Notice that this path does not point to your application. It points to your project directory. Your plist is supposed to be at
@/Users/andrewf/MyApp/build/Release/MyApp.app/Resources/Plist.plist"
(for an iPhone app this would be different, more like@"/Users/andrewf/Library/Application Support/iPhone Simulator/User/Applicatons/(unique id)/MyApp.app/Resources/Plates.plist"
), and it is inside the Resources folder of your app thatpathForResource:
is looking.What this implies is that your resource is not in the "Copy Bundle Resources" phase of your build target. You need to drag it from the Groups and Files area to inside that phase.
我已经找到问题了!
.plist 文件已正确包含在目标中。
最初创建 .plist 时,我将文件命名为“plates.plist”。创建文件后,我立即将其重命名为“Plates.plist”,这就是我以后使用的。
尽管该文件在我的资源文件夹中被命名为“Plates.plist”,但在目标部分和上面提到的 iPhone 模拟器位置中,该文件被命名为“plates.plist”。奇怪的是,文件的内容仍然被正确更新。
现在我已经更改了代码以引用“plates.plist”,并将资源文件夹中的文件重命名为相同的文件以进行良好的测量,一切正常。我只能假设这是 iPhone SDK 中的一个错误。
I have found the problem!
The .plist file was included in the target correctly.
When the .plist was originally created, I named the file "plates.plist". Immediately after creating the file, I renamed it to "Plates.plist", and this is what I used henceforth.
Even though the file was named "Plates.plist" in my resources folder, in the target section and in the iPhone Simulator location mentioned above, the file was named "plates.plist". Curiously, the contents of the file was still updated correctly.
Now that I have changed the code to refer to "plates.plist", and renamed the file in the Resources folder to the same for good measure, everything works. I can only assume that this is a bug in the iPhone SDK.
检查
plates.plist
文件是否已添加到 Xcode 中的目标中。如果该文件未添加到目标中,它将不会位于构建产品中,并且 NSBundle 将找不到它。
Check that the
plates.plist
file has been added to the target in Xcode.If the file is not added to the target it will not be inside the build product and NSBundle will not find it.