Obj-c 静态库中的 NSBundle、plist 和其他资源
我在 Xcode 中创建了一个静态库,我可以在其他项目中成功使用它。然而,对于像 plists 这样的资源,我发现我必须将我的库中引用的任何 plists 包含在使用该项目的主项目中。
在我的静态库项目中,我将 plist 包含在目标的“复制捆绑资源”阶段中。在我的代码中,这就是我正在做的事情:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"MyClassParams" ofType:@"plist"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
如果我使用 mainBundle 并且 MyClassParams.plist 包含在主项目中,则一切都很好。如果 MyClassParams.plist 包含在库项目中,则它不起作用。
假设 [NSBundle mainBundle] 引用了错误的静态方法,我将其替换为:
NSBundle *mainBundle = [NSBundle bundleForClass:[MyClass class]];
这也不起作用。
那么,是否可以在静态库中包含 plist 或任何其他资源——或者我是否必须在使用该库的项目中包含我需要的任何内容?
I've created a static library in Xcode, which I am able to successfully use in other projects. However, with resources like plists, I find I must include any plists referenced in my library in the main project where the project is used.
In my static library project, I have my plist included in the "Copy Bundle Resources" phase of the target. In my code, here is what I am doing:
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"MyClassParams" ofType:@"plist"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
If I use mainBundle and the MyClassParams.plist is included in the main project, all is good. If MyClassParams.plist is included in the library project, it doesn't work.
On the assumption that [NSBundle mainBundle] was referencing the wrong static method to use, I replaced it with:
NSBundle *mainBundle = [NSBundle bundleForClass:[MyClass class]];
This did not work either.
So, is it possible to include a plist or any other resources with a static library -- or do I have to include whatever I need in the project where the lib is used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态库不在捆绑包中,当它们链接到应用程序时,它们是该应用程序捆绑包的一部分。在 iPhone 上,实际上您编写的所有代码都将位于 mainBundle 中,因为您不能包含嵌入式框架。
所以是的,您需要将所有资源复制到要将静态框架链接到的项目中。
Static Libraries are not in bundles, when they get linked into an application they are part of that applications bundle. On the iPhone, effectively all code you write will be in the mainBundle since you can't include embedded frameworks.
So yes, you need to copy over all the resources into the project you are linking the static framework into.
我知道您不能将资源文件包含到静态库中(这就是框架所做的)。
我在我的项目中使用另一个解决方案:
在静态库“YYY”项目内部:
主项目内部:
libYYY.a
YYY.bundle
添加到复制的资源文件中这样,静态库中使用的资源文件就不会被管理在主要项目。假设我在静态库中有一张
foo.png
图片,我使用你可以得到这样的 plist:
下面,两个屏幕截图:
I know you can't include resource files into a static library (that's what frameworks do).
I use another solution in my projects:
Inside the static library "YYY" project:
Inside the main project:
libYYY.a
YYY.bundle
to the copied resource filesThis way, resource files used in the static library are not managed in the main project. Say I have a
foo.png
picture in the static library, I useYou could then get your plist like this:
Below, two screenshots:
我遵循了 @Jilouc 建议的所有内容,但是,我可以获取捆绑包,但无法获取其中的文件。我尝试了两种方法(
@"yyy.bundle/image"
或staticlib pathforresource...
)然后我使用了下面的方法,它起作用了!
I followed everything @Jilouc suggested, however, I can get the bundle but failed to get files inside it. I tried both the two ways (
@"yyy.bundle/image"
orstaticlib pathforresource...
)Then I used the method below and it worked!