我试图将一组资源捆绑到一个捆绑包中,并且他们通过NSBundle从代码访问它们。为什么我的包忽略了它的info.plist?
到目前为止,我可以很好地从捆绑包中获取资源,但是我无法让NSBundle实例从捆绑的Info.plist文件中获取任何元数据。
奇怪的是,我的第一次尝试工作,并正确地获取元数据,但自那时以来,我一直无法复制这种行为,并且捆绑包总是返回null为bundleIdentifier或infoDictionary中的其他项目。
我已代码减少到下面的测试情况:
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
图像资源加载正确,但val在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>
我缺少的东西?我究竟做错了什么?
非常感谢。
我从来没有使用捆绑。但是,并不是每个软件包都包含“内容”文件夹吗?为什么你使用包? – lbrndnr
辉煌 - 我忘记了内容文件夹。它现在有效,非常感谢。 – Diggory
添加了一个答案,所以它的官方:) – lbrndnr