ObB-c静态库中的NSBundle,plist和其他资源 [英] NSBundle, plist and other resources in an Obj-c Static Library

查看:108
本文介绍了ObB-c静态库中的NSBundle,plist和其他资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Xcode中创建了一个静态库,我可以在其他项目中成功使用它。但是,对于像plists这样的资源,我发现我必须在我的库中使用项目中使用的任何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.

在我的静态库项目中,我让我的plist包含在目标的Copy Bundle Resources阶段。在我的代码中,这是我正在做的事情:

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];

如果我使用mainBundle并且MyClassParams.plist包含在主项目中,一切都很好。如果MyClassParams.plist包含在库项目中,则它不起作用。

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.

假设[NSBundle mainBundle]引用了错误的静态方法,我将其替换为:

On the assumption that [NSBundle mainBundle] was referencing the wrong static method to use, I replaced it with:

NSBundle *mainBundle = [NSBundle bundleForClass:[MyClass class]];

这也不起作用。

那么,是否可以将plist或任何其他资源包含在静态库中 - 或者我必须在lib所在的项目中包含我需要的内容使用?

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?

推荐答案

静态库不在捆绑中,当它们链接到应用程序时,它们是该应用程序包的一部分。在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.

这篇关于ObB-c静态库中的NSBundle,plist和其他资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆