我有一個.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中,使得它被附加到以前的和不覆蓋寫入數據?
http://stackoverflow.com/questions/8884215/appending-data-to-plist-iphone – the1pawan
@Bhumeshwerkatre所以關閉這個問題,而不是僅僅發表評論。 – trojanfoe
@ user3040536 http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/,檢查上面的鏈接可能會幫助你。 –