2013-11-27 31 views
0

我有一個.plist文件。當我將數據寫入plist時,它不會追加,而是覆蓋以前的內容。如何在plist中添加數據?

這裏是我的代碼,

// get paths from root direcory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our Data/plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"HistoryList.plist"]; 

// set the variables to the values in the text fields 
NSString *str=[NSString stringWithFormat:@" %@ %@ %@",strname.text,strnum.text,selectfield.text]; 
self.historyArray = str; 

// create dictionary with values in UITextFields 
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: historyArray, nil] forKeys:[NSArray arrayWithObjects: @"Name", nil]]; 

NSString *error = nil; 
// create NSData from dictionary 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

// check is plistData exists 
if(plistData) 
{ 
    // write plistData to our Data.plist file 
    [plistData writeToFile:plistPath atomically:YES]; 
} 
else 
{ 
    NSLog(@"Error in saveData: %@", error); 

} 

如何在plist中,使得它被附加到以前的和不覆蓋寫入數據?

+0

http://stackoverflow.com/questions/8884215/appending-data-to-plist-iphone – the1pawan

+0

@Bhumeshwerkatre所以關閉這個問題,而不是僅僅發表評論。 – trojanfoe

+0

@ user3040536 http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/,檢查上面的鏈接可能會幫助你。 –

回答

0
  1. 閱讀的.plist文件和負載作爲字典
  2. 修改字典與你的新項目
  3. 保存完整的字典替換舊文件
+0

但我該怎麼做? – user3040536

+0

NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile:plistPath]; – Traze