如何创建正确的包

如何创建正确的包

本文介绍了在 iOS 项目中使用包时出错/如何创建正确的包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 项目中包含一个静态库.我导入了整个静态库项目,然后在目标依赖项"和将二进制文件与库链接"中链接了库,如下所述:http://www.applausible.com/blog/?p=657

I'm trying to include a static library in an iOS project. I imported the whole static library project then I linked the lib in the 'target dependencies' and 'link binary with libraries' as that's explained here: http://www.applausible.com/blog/?p=657

但是我在测试应用程序时收到此错误:

But then I'm getting this error when testing the app :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/alexis/Library/Application Support/iPhone Simulator/5.0/Applications/9811107D-0D7E-4178-ACCE-BF43E3043770/PlazappPartnerTest.app> (loaded)' with name 'LauncherView''
*** First throw call stack:
(0x188a052 ......

我猜它无法访问 .xib 文件,但由于它包含在我的库中,我不知道为什么.如何让主项目看到".xib 文件?(如果这真的是问题所在...)

I guess it can't access the .xib file but since it's included in my library I don't know why.How do I make the main project 'see' the .xib file? (If that's actually the problem...)

我尝试制作一个包含 .xib 文件的包,并将其包含到我的主项目中,但我无法使用此包.我试过这个:

I tried to make a bundle containing the .xib files and I included it to my main project but I can't use this bundle. I tried this :

NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"BundleName" ofType:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

NSError *error;
[bundle loadAndReturnError:&error];

NSLog(@"bundle error : %@", [error userInfo]);

我收到了这个错误:

NSLocalizedDescription = "The bundle \U201cBundleName.bundle\U201d couldn\U2019t be loaded because its executable couldn\U2019t be located.";
NSLocalizedFailureReason = "The bundle\U2019s executable couldn\U2019t be located.";
NSLocalizedRecoverySuggestion = "Try reinstalling the bundle.";

有什么问题吗?

我想我无法加载包含非可执行资源(如 .xib 文件)的包.我说得对吗?

I think I can't load a bundle containing non-executables resources like .xib files. Am I right?

但是,如何从包中加载我的 .xib?

But then, how can load my .xib from the bundle?

推荐答案

在 Cocoa 编程中,通常有不止一种方法可以获取捆绑包.我已经成功了:

Frequently in Cocoa programming there's more than one way to get a bundle. I've had success with this:

NSBundle *libBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"BundleName" withExtension:@"bundle"]];

然后用这个加载笔尖:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:libBundle];

您对loadAndReturnError: 的这个特定调用加载了包的可执行代码.由于这个包可能没有任何可执行代码,只有库需要的资源集合,它会给你这个错误.

This specific call you make to loadAndReturnError: loads the bundle's executable code. As this bundle probably doesn't have any executable code, just the collection of resources the library needs, it will give you this error.

编辑(回复评论):

我通过向包的库项目添加新目标来制作包.从 Mac OS X 包模板开始,我将所有图像、.xib 和其他资源添加到复制包资源"构建阶段.我将基础 SDK 更改为 iOS(最新).然后我将包添加到库的依赖项和最终应用程序的复制包资源"构建阶段.

I made my bundle by adding a new target to my library project for the bundle. Starting with the Mac OS X bundle template, I added all my image, .xib, and other resources to the "Copy Bundle Resources" build phase. I changed the base SDK to iOS (latest). Then I added the bundle to the library's dependencies and final app's "Copy Bundle Resources" build phase.

如果只是尝试将 .xib 文件添加到文件夹的方法,则 .xib 不会编译为 nib,iOS 无法读取它们.

If you just try the add .xib files to a folder method, the .xibs are not compiled into nibs, and iOS can't read them.

这篇关于在 iOS 项目中使用包时出错/如何创建正确的包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:53