Writing a new set of data to Plist instead of overwriting it(将一组新数据写入 Plist 而不是覆盖它)
问题描述
I'm trying to get a plist to store multiple sets of data, but each time I save (using a button from an ActionSheet), it overwrites the previous set. I want to add multiple 'friends' and their data. I'm not too keen on using Core Data, so I'm wondering how you can do it with a Plist.
Here is the code for the save button:
NSMutableDictionary *friend = [[NSMutableDictionary alloc] init];
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:friendName.text forKey:@"Name"];
[array setObject:friendPhone.text forKey:@"Phone Number"];
[array setObject:friendEmail.text forKey:@"Email Address"];
[array setObject:friendChatSN.text forKey:@"Chat Screen Name"];
[friend setValue:array forKey: @"Friend Data"];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"FriendList.plist"];
[friend writeToFile:plistPath atomically: TRUE];
Instead of overwriting it, how can I modified the code to add a new set of friend data each time I save?
Here pList behave as an one object. so whenever you try to write something to plist it will be replaced with new data.
Look like you want to save multiple friends record to plist file. Best solution for your question is to use core data or sqlite. But if you want to achieve it using plist, then first get old data from plist and the add new data (friends) to old data then saving to plist should work out.
As per your requirement you should save NSArray
with multiple NSDictionary
(friend record) object to pList.
Here is the modified code
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"FriendList.plist"];
NSMutableArray *friend = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == friend) {
friend = [[NSMutableArray alloc] initWithCapacity:0];
}
else {
[friend retain];
}
NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
[array setObject:friendName.text forKey:@"Name"];
[array setObject:friendPhone.text forKey:@"Phone Number"];
[array setObject:friendEmail.text forKey:@"Email Address"];
[array setObject:friendChatSN.text forKey:@"Chat Screen Name"];
[friend addObject:array];
[array release];
[friend writeToFile:plistPath atomically: TRUE];
[friend release];
这篇关于将一组新数据写入 Plist 而不是覆盖它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将一组新数据写入 Plist 而不是覆盖它
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01