为什么我的包忽略了它的 info.plist?
我试图将一组资源捆绑到一个包中,然后它们通过 NSBundle 从代码中访问它们。
到目前为止,我可以很好地访问捆绑包中的资源,但我无法让 NSBundle 实例从捆绑包的 Info.plist 文件中获取任何元数据。
奇怪的是,我的第一次尝试成功了,并且正确地获取了元数据,但从那时起我就无法复制此行为,并且捆绑包始终为捆绑包标识符或 infoDictionary 中的其他项目返回 null。
我已将代码简化为以下测试用例:
NSString *pathToTestBundle = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"testBundle"];
NSLog(@"path to Test Bundle: %@", pathToTestBundle);
NSBundle *testBundle = [NSBundle bundleWithPath: pathToTestBundle];
NSLog(@"testBundle: %@", testBundle);
NSString *identifier = [testBundle bundleIdentifier];
NSLog(@"testBundle identifier: %@", identifier);
NSURL *testResourceURL = [testBundle URLForResource:@"vanGroup"
withExtension:@"png"];
NSLog(@"testBundle test resource: %@", testResourceURL);
NSImage *testImage = [[NSImage alloc] initByReferencingURL:testResourceURL];
[imageView setImage:testImage];
NSLog(@"%@", [testBundle infoDictionary]);
给出了以下内容:(已编辑行长度)
path to Test Bundle: /.../BundleTester.app/Contents/Resources/test.testBundle
testBundle: NSBundle </../BundleTester.app/Contents/Resources/test.testBundle>
(not yet loaded)
testBundle identifier: (null)
testBundle test resource: file://.../BundleTester.app/Contents/Resources/test.testBundle/Resources/vanGroup.png
BundleTester[45083:707] {
NSBundleInitialPath = "/Users/diggory/Library/Developer/Xcode/DerivedData/BundleTester-hcbsnhudsdfzlyggjbvlkdbrtxrw/Build/Products/Debug/BundleTester.app/Contents/Resources/test.testBundle";
NSBundleResolvedPath = "/Users/diggory/Library/Developer/Xcode/DerivedData/BundleTester-hcbsnhudsdfzlyggjbvlkdbrtxrw/Build/Products/Debug/BundleTester.app/Contents/Resources/test.testBundle";
}
在我的 Xcode 项目中,测试包被添加为文件夹引用并具有以下结构:
test.testBundle
Info.plist
Resources
vanGroup.png
图像资源已正确加载,但值plist 中的内容将被忽略。
plist 如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.monkeyfood.testbundle</string>
<key>badger</key>
<string>Bodger</string>
</dict>
</plist>
我错过了什么吗?我做错了什么?
非常感谢。
I am attempting to bundle a set of resources into a bundle, and them access them from code via NSBundle.
So far, I can access the resources from the bundle fine, but I cannot get the NSBundle instance to get any meta-data from the bundle's Info.plist file.
Strangely, my first attempt worked, and correctly got the meta-data, but since then I have been unable to replicate this behaviour and the bundle always returns null for the bundleIdentifier or other items in the infoDictionary.
I have reduced the code to the following test case:
NSString *pathToTestBundle = [[NSBundle mainBundle] pathForResource:@"test"
ofType:@"testBundle"];
NSLog(@"path to Test Bundle: %@", pathToTestBundle);
NSBundle *testBundle = [NSBundle bundleWithPath: pathToTestBundle];
NSLog(@"testBundle: %@", testBundle);
NSString *identifier = [testBundle bundleIdentifier];
NSLog(@"testBundle identifier: %@", identifier);
NSURL *testResourceURL = [testBundle URLForResource:@"vanGroup"
withExtension:@"png"];
NSLog(@"testBundle test resource: %@", testResourceURL);
NSImage *testImage = [[NSImage alloc] initByReferencingURL:testResourceURL];
[imageView setImage:testImage];
NSLog(@"%@", [testBundle infoDictionary]);
Which gives this: (edited for line length)
path to Test Bundle: /.../BundleTester.app/Contents/Resources/test.testBundle
testBundle: NSBundle </../BundleTester.app/Contents/Resources/test.testBundle>
(not yet loaded)
testBundle identifier: (null)
testBundle test resource: file://.../BundleTester.app/Contents/Resources/test.testBundle/Resources/vanGroup.png
BundleTester[45083:707] {
NSBundleInitialPath = "/Users/diggory/Library/Developer/Xcode/DerivedData/BundleTester-hcbsnhudsdfzlyggjbvlkdbrtxrw/Build/Products/Debug/BundleTester.app/Contents/Resources/test.testBundle";
NSBundleResolvedPath = "/Users/diggory/Library/Developer/Xcode/DerivedData/BundleTester-hcbsnhudsdfzlyggjbvlkdbrtxrw/Build/Products/Debug/BundleTester.app/Contents/Resources/test.testBundle";
}
In my Xcode project by test bundle is added as a folder reference and has the following structure:
test.testBundle
Info.plist
Resources
vanGroup.png
The image resource is loaded correctly, but the values in the plist are ignored.
The plist is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>com.monkeyfood.testbundle</string>
<key>badger</key>
<string>Bodger</string>
</dict>
</plist>
Am I missing something? What am I doing wrong?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
缺少“Contents”文件夹。
The "Contents" folder is missing.
看来您的
bundle
对象引用了 Bundle,但 Bundle 尚未加载,这就是为什么您在调用 bundleIdentifier 时得到 null 的原因。来自苹果关于捆绑加载代码的文档您需要像这样调用
[bundle load]
:有关 捆绑加载 在 Apple 文档中进行了解释
It seems your
bundle
object is referring to the Bundle but the Bundle isn't yet loaded which is why you get null when you call bundleIdentifier. From Apple's Documentation on Bundle Loading CodeYou need to call
[bundle load]
like so:More information on Bundle Loading is explained in Apple's Documentation