2011-03-31 47 views
1

我使用的XCode 4斜面加载文件

可有人请向我解释为什么当我写了一个单元测试(测试目标下),它无法找到我本地文件资源?我试图在单元测试中从NSBundle加载一个plist文件,但它不适用于单元测试目标。我试过把它扔在Test目标的Classes区域下面。

这里是我的代码:

NSString *path = [[NSBundle mainBundle] bundlePath]; 
NSString *finalPath = [path stringByAppendingPathComponent:@"urlMappings.plist"]; 
self.urlMappings = [NSDictionary dictionaryWithContentsOfFile:finalPath]; 

self.urlMappings是空的,即使urlMappings.plist在我的项目肯定存在着一些原因。这里是urlMappings.plist的来源,以证明它实际上是一本字典。

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>%server%view.html</key> 
    <string>testHtml1.html</string> 
</dict> 
</plist> 

任何想法? Thankyou

+0

大厦关闭MikeSimmons的解决方案,我想出了下面的[Xcode中:TEST VS DEBUG预处理宏(http://stackoverflow.com/questions/6748087/xcode-test-vs-debug-preprocessor-macros/6763597 #6763597)。 – ma11hew28 2011-07-20 14:40:46

回答

7

在测试目标下,不应该使用[NSBundle mainBundle],因为它是主包,而不是测试包。你应该使用[NSBundle bundleForClass:[self class]]替换。

+0

非常有意义。我已经给你解决方案。谢谢! – 2013-08-26 06:21:17

0

解决方案(没有意义)是不是主要目标的捆绑资源。即使我仅在测试中使用它,它也必须位于两个目标的束资源中。

任何人都知道为什么会出现这种情况?