如何在plist中附加数据?

时间:2013-11-27 06:56:12

标签: ios iphone objective-c 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中写入数据,使其附加到上一个并且不会覆盖?

1 个答案:

答案 0 :(得分:0)

  1. 阅读.plist文件并加载为字典
  2. 使用新项目修改字典
  3. 保存替换旧文件的完整字典