我已经创建了一个静态的图书馆的模式,我能够成功地使用在其他项目。然而,与资源,如plist,我发现我必须包括任何plist引用在我的图书馆的主要项目,该项目使用。

在我的静态图书馆项目,我有我的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或任何其他资源与一个静态的图书馆--或者我必须包括任何我需要的项目lib是使用?

有帮助吗?

解决方案

静态库不在束,当他们获取链接到应用中,它们是应用程序包的一部分。在iPhone上,有效地编写所有的代码将在mainBundle因为你不能包括嵌入式框架。

所以,是的,你需要在所有的资源复制到您链接静态框架到项目中。

其他提示

我知道你不能包括资源的文件变成一个静态的图书馆(这是什么框架)。我用另外的方案在我的项目:

内部的静态图书馆"YYY"项目:

  • 我增加一个"可装载的包"的目标到我的静态图书馆项目(加的目标、可可、可装载束)
  • 我添加这一目标作为一个依赖性的静态图书馆的目标

内部的主要项目:

  • 链接 libYYY.a
  • 添加捆绑 YYY.bundle 以复制的资源文件

这种方式,资源文件中使用的静态图书馆管理的主要项目。说我有一个 foo.png 画中的静态图书馆,使用我

[UIImage imageNamed:@"YYY.bundle/foo.png"]

然后您可以让你plist这样的:

NSBundle *staticLibBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"YYY" ofType:@"bundle"]];
NSString *filePath   = [staticLibBundle pathForResource:@"MyClassParams" ofType:@"plist"];

NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

下面,两个截图:

  • :静态图书馆项目载束目标
  • :表示静态图书馆二添加到"链接二进制与图书馆"和资源包加入"复制捆绑的资源"。

Static library project Main project

我也跟着一切@Jilouc建议,不过,我可以得到一束但未能获得里面的文件。我尝试都两种方式(@"yyy.bundle/image"staticlib pathforresource...

然后我用下面的方法和它的工作!

NSBundle *staticBundle = [NSBundle bundleWithPath:[[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:@"yy.bundle"]];

NSString *filePath = [[jsBundle resourcePath]stringByAppendingPathComponent:fileName];

NSString* content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top