使用 [[NSBundle mainBundle] pathForResource 获取 .plist 路径时出现问题

发布于 2024-08-11 02:47:59 字数 642 浏览 10 评论 0 原文

我是一个 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 。

我已经确认该文件存在于正确的位置,甚至重新创建了它以确保确定。

这看起来是一个很简单的问题,但我很困惑。请帮忙。

I'm an Objective C noob, and I don't know enough to explain the following problem.

This code works:

NSString *plistPath = @"/Users/andrewf/MyApp/Resources/Plates.plist";
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

My dictionary object is loaded with values as expected.

This code does not work:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Plates" ofType:@"plist"];
dicPlates = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

plistPath comes back with a value of nil. This is the case irrespective of whether I include inDirectory:@"Resources" in the call or not. All the examples that I have found do not include inDirectory when trying to open a .plist file in the Resources directory.

I have confirmed that the file exists in the correct location and even recreated it to be sure.

This seems like such a simple problem, but I am mystified. Please assist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

银河中√捞星星 2024-08-18 02:47:59

我认为您对 pathForResource: 在哪里寻找感到困惑。

这是可行的:

NSString *plistPath = @"/Users/andrewf/MyApp/Resources/Plates.plist";

请注意,该路径并不指向您的应用程序。它指向您的项目目录。您的 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:

NSString *plistPath = @"/Users/andrewf/MyApp/Resources/Plates.plist";

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 that pathForResource: 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.

—━☆沉默づ 2024-08-18 02:47:59

我已经找到问题了!

.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.

谈下烟灰 2024-08-18 02:47:59

检查 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文