我有一个由应用程序链接到的静态库。库代码打开应用程序包中的包中的文件,打开方式如下:
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"Config" ofType:@"plist"];
这工作正常。
但是我想向库中添加一些单元测试代码,所以我有一个逻辑测试目标。由于该文件位于应用程序的 bundle 中,而不是静态库的 bundle 中,因此我复制了 Config.plist
文件并通过 Copy Bundle Resources将其添加到测试代码目标中
。但是当我执行测试代码时找不到文件。这是为什么?
上面的内容令人困惑,这里是工作区结构的摘要。
Workspace contains:
Application Project with application target, which contains (X)
Config.plist (a)
Library project which contains:
Library target, which contains:
the code opening the file in the bundle (b)
Test library target, which contains: (Y)
A Copy of the Config.plist (c)
因此,如果我构建 X,那么当 b 运行时,它可以找到 a。但是当我在运行时构建 Y 时,b 找不到 c。
最佳答案
我发现如果我将 [Bundle mainBundle] 更改为 [NSBundle bundleForClass:[self class]] 那么它在两种情况下都有效
关于ios - 如何在测试目标中添加/打开捆绑文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16310660/