2011-06-02 79 views
6

我有一個數組的plist包含2串看到圖像:arrayWithContentsOfFile無法讀取plist文件

http://imageshack.us/photo/my-images/263/myplist.png/

,並在我的viewDidLoad我補充一點:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 

NSArray *array = [NSArray arrayWithContentsOfFile:file]; 

NSLog(@"array = %@", array); 

但我爲什麼總是得到null?感謝幫助。

UPDATE:解決的辦法是,你需要手動編輯plist文件(Xcode4使用Dictionnary默認)

<?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"> 
<array> 
    <array> 
     <string>HO</string> 
     <string>HI</string> 
    </array> 
</array> 
</plist> 
+0

發佈你的完整代碼.. – Aravindhan 2011-06-02 04:49:00

+0

@funnyCoder你檢查路徑是否存在?也許在那裏貼一個[[[NSFileManager defaultManager] fileExistsAtPath:file]'? – 2011-06-02 04:51:24

+0

@Aravindhanarvi這是我的完整代碼:)。 – funnyCoder 2011-06-02 04:52:40

回答

13

最有可能的,你的plist是包含數組的字典(4 Xcode中不允許創建一個數組作爲根對象)。嘗試是這樣的:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 
NSArray *array = [dict objectForKey:@"Array"]; 
NSLog(@"%@", array); 
+0

謝謝,但總是空! – funnyCoder 2011-06-02 04:55:56

+0

對不起,謝謝你沒關係:)。但你能解釋爲什麼這樣嗎? – funnyCoder 2011-06-02 04:57:11

+0

@omz老兄它允許創建實際上允許創建數組作爲根 – NIKHIL 2011-06-02 04:59:30

0

plist should start with Root Key

在你的應用程序,你需要更改根密鑰名稱,因爲它不能是數據類型

+0

我改變了它,但沒有成功,似乎Xcode 4不允許數組plist ?! – funnyCoder 2011-06-02 05:01:55

+0

Ohhh是的,它就是這樣 – NIKHIL 2011-06-02 05:05:37

+0

所以hiw來處理?你可以給我一個關於Xcode4 Array plist支持的網址嗎?謝謝。 – funnyCoder 2011-06-02 05:10:32

0

我已經研究過這個話題,發現在一些差異plist文件。

例如,我在樣本代碼的原始plist文件被讀取與 方法
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

在xml原始文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 

<plist version="1.0"> 

<array> 
<dict> 
    <key>nameKey</key> 
    <string>Number One</string> 
    <key>imageKey</key> 
    <string>small_one.png</string> 
</dict> 
<dict> 
    <key>nameKey</key> 
    <string>Number Two</string> 
    <key>imageKey</key> 
    <string>small_two.png</string> 
</dict> 

</array>   
</plist> 

當我試圖創建此相同上面的文件在Xcode界面中,使用添加新的文件屬性列表,並且在編輯後將該項目以同樣的方式顯示在上述文件的界面中,則方法[[NSArray alloc] initWithContentsOfFile:path];失敗,並且我在xml plist文件的結構中發現了該問題。 使用的XCode接口創建的文件,導致XML plist文件:

<plist version="1.0"> 
<dict> 
<key>item 0</key> 
<dict> 
    <key>imageKey</key> 
    <string>image1.jpg</string> 
</dict> 
<key>item 1</key> 
<dict> 
    <key>imageKey</key> 
    <string>image2.jpg</string> 
</dict> 
</dict> 
</plist> 

我固定更改xml文件直接作爲相同的第一個例子,該方法工作得很好。

點是在plist文件的XCode接口,你不能看到這些差異。兩者看起來都一樣。也許是我的Xcode 4.1

0

正確的答案是,根項目已經是一個數組(在XCode4 ..不知道其他人)。所以如果你把另一個數組作爲neil d顯示在他的屏幕截圖中,你最終會得到一個包含兩個字符串值的數組的數組。

0

這種類型的plist

enter image description here

OR(基本上都是相同的)

enter image description here

可以這樣寫:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
_arrFeats = [NSArray arrayWithContentsOfFile:file]; 
NSLog(@"%i", _arrFeats.count); 

appFeaturesplist的名稱。 (iOS 6.1)