如何在 plist 中追加数据? [复制]
Posted
技术标签:
【中文标题】如何在 plist 中追加数据? [复制]【英文标题】:How to append data in plist? [duplicate] 【发布时间】:2013-11-27 06:56:12 【问题描述】:我有一个 .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 中写入数据,使其附加到上一个并且不会覆盖?
【问题讨论】:
***.com/questions/8884215/… @Bhumeshwerkatre 所以结束这个问题,而不仅仅是发表评论。 @user3040536 ipgames.wordpress.com/tutorials/writeread-data-to-plist-file,查看上面的链接可能会对您有所帮助。 【参考方案1】:-
读取 .plist 文件并作为字典加载
用您的新项目修改字典
保存替换旧文件的完整字典
【讨论】:
但是我该怎么做呢?? NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: plistPath]; 如何用 swift 执行以上是关于如何在 plist 中追加数据? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
在文档目录中找不到 plist 文件?如何以编程方式修改 plist?
如何在 android listview 中显示 Plist 数据