【问题标题】:Set 'Host Application' to None for test target, then unable to find bundle resources将测试目标的“主机应用程序”设置为无,然后无法找到捆绑资源
【发布时间】:2016-07-20 09:05:39
【问题描述】:

在将 Host Application 设置为 none 之前,我的单元测试中有这行代码,这没有给我带来任何问题。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

设置为 None 后,出现此错误。

<unknown>:0: failed: caught “NSInvalidArgumentException”, “Could not find a
storyboard named ‘Main’ in bundle NSBundle 
</Applications/Xcode.app/Contents/Developer/Platforms/
iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator9.2.sdk/Developer/usr/bin> (loaded)”

我找到了this post,它给了我解决方案。我刚刚指定了我正在加载视图控制器的类的捆绑包。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" 
bundle:[NSBundle bundleForClass:[self class]];

我只是想知道为什么会这样。当我删除导致主包中的资源被排除的主机应用程序时会发生什么?

【问题讨论】:

    标签: ios unit-testing storyboard xctest nsbundle


    【解决方案1】:

    没关系,我知道为什么。 mainBundle 的文档说

    返回对应于当前应用程序可执行文件所在目录的 NSBundle 对象。

    由于我删除了主机应用程序,我的项目找不到主包目录,因为没有应用程序可执行文件。

    【讨论】:

    • 我经常想知道“主机应用程序”做了什么但没有看到任何变化,因为我习惯于以第二种方式编写我的测试代码。现在我知道了!
    猜你喜欢
    • 1970-01-01
    • 2012-04-14
    • 2015-06-22
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多