2011-03-31 44 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

解決方案(沒有意義)是不是主要目標的捆綁資源。即使我僅在測試中使用它,它也必須位於兩個目標的束資源中。

任何人都知道爲什麼會出現這種情況?