【问题标题】:What is interpretation for bundleForClass returns nil and how that can be corrected?bundleForClass 返回 nil 的解释是什么以及如何纠正?
【发布时间】:2013-10-13 22:57:28
【问题描述】:

我正在准备 XCTest 并执行测试,我需要从测试包中加载资源。因此我使用:

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"item" ofType:@"plist"];

问题是bundleForClass 返回nil。这是怎么发生的,因为每个类都绑定到从中加载的包?是否有任何设置需要更正以使其正常工作?

【问题讨论】:

  • 你找到解决这个问题的方法了吗?

标签: ios objective-c unit-testing tdd nsbundle


【解决方案1】:

你可以这样做,

NSString *path = [[NSBundle mainBundle] pathForResource:@"item" ofType:@"plist"];

【讨论】:

  • [NSBundle allBundles] 也返回 nil
  • 那么item.plist 在哪里?它在项目中吗?
  • 它通过 Xcode 包含在 UnitTest 包中。从磁盘上的序列化数据创建对象的测试数据是测试数据,所以它不是原始应用程序的一部分。
  • 以下行不是问题,因为当我得到正确的捆绑包时,它会找到合适的路径
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 2022-06-20
  • 1970-01-01
  • 2019-02-10
  • 2021-10-02
  • 2020-01-20
  • 2014-04-23
  • 2018-01-27
相关资源
最近更新 更多