2011-08-31 37 views
1

我有一個數組和一些字符串在數組中的plist。所以我想知道如何在plist中向數組寫一個新的字符串。當我使用下面的代碼從plist獲取字符串時,我只是在日誌中得到空值。在plist中寫入字符串

NSMutableArray *myPrimaryinfo = [[NSMutableArray alloc] initWithContentsOfFile:@"/private/var/mobile/Library/Keyboard/UserDictionaryWordKeyPairs.plist"]; 

NSLog(@"%@", myPrimaryinfo); 

我的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"> 
<array> 
    <string>Test string1</string> 
    <string>Test string2</string> 
    <string>Test string3</string> 
    <string>Test string4</string> 
</array> 
</plist> 

回答

2

讓你的文件的路徑如下圖所示,並嘗試..

NSString *path = [[NSBundle mainBundle] pathForResource:@"DrinkArray" ofType:@"plist"]; 
+0

的plist中的實際路徑是:/private/var/mobile/Library/Keyboard/UserDictionaryWordKeyPairs.plist –

+0

primaryList.plist只是一個如果 –

+0

其實你需要的副本只在項目中包含plist中,每當你會編譯項目plist將被包含在模擬器路徑中... – DShah

0

你不能這樣做iOS上,因爲文件系統在您應用程序的沙箱外不透明。關於什麼是可在iOS的文件系統中您的應用程序的詳細信息,請閱讀本:

http://developer.apple.com/library/iOS/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/RuntimeEnvironment/RuntimeEnvironment.html#//apple_ref/doc/uid/TP40007072-CH2-SW12

而不是試圖訪問不可用的到你的應用程序中的文件系統的部分,考慮在你的應用程序的plist文件束。如果您希望應用程序能夠動態修改plist,則可以將其副本保存到與應用程序包平行的Documents目錄中,然後從該位置讀取plist。

+0

Okey,可以說plist包含在我的應用程序包中,那麼如何將新字符串添加到plist中的數組? :) –

+0

這是一個完全不同的問題,並已被回答很多次,所以在這裏例如:http://stackoverflow.com/questions/2292347/creating-a-plist-file-programatically – jlehr

+0

一個偉大的地方開始雖然,蘋果的文檔:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/CreatePropListProgram/CreatePropListProgram.html – jlehr