简体   繁体   English

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

[英]Problem getting path to .plist using [[NSBundle mainBundle] pathForResource

I'm an Objective C noob, and I don't know enough to explain the following problem. 我是一个Objective C菜鸟,我不太了解以下问题。

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. plistPath返回值为nil。 This is the case irrespective of whether I include inDirectory:@"Resources" in the call or not. 无论我是否在调用中包含inDirectory:@“Resources”,情况都是如此。 All the examples that I have found do not include inDirectory when trying to open a .plist file in the Resources directory. 尝试在Resources目录中打开.plist文件时,我发现的所有示例都不包含inDirectory。

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. 请协助。

I think you're confused as to where pathForResource: is looking. 我觉得你对于pathForResource:正在寻找的地方感到困惑。

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. 您的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:正在查找。

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. 您需要将它从“组和文件”区域拖到该阶段内。

I have found the problem! 我发现了问题!

The .plist file was included in the target correctly. .plist文件正确包含在目标中。

When the .plist was originally created, I named the file "plates.plist". 最初创建.plist时,我将文件命名为“plates.plist”。 Immediately after creating the file, I renamed it to "Plates.plist", and this is what I used henceforth. 创建文件后,我立即将其重命名为“Plates.plist”,这就是我今后使用的内容。

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". 即使该文件在我的资源文件夹,目标部分和上面提到的iPhone模拟器位置中被命名为“Plates.plist”,该文件也被命名为“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. 现在我已经更改了代码以引用“plates.plist”,并将Resources文件夹中的文件重命名为相同以便进行测量,一切正常。 I can only assume that this is a bug in the iPhone SDK. 我只能假设这是iPhone SDK中的一个错误。

Check that the plates.plist file has been added to the target in Xcode. 检查plates.plist文件是否已添加到Xcode中的目标。

If the file is not added to the target it will not be inside the build product and NSBundle will not find it. 如果文件未添加到目标,则它不会在构建产品中,并且NSBundle将找不到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM