数据持久化-写入plist
来源:互联网 发布:网络歌手唱的无路可退 编辑:程序博客网 时间:2024/04/29 06:56
需要确定我们想要存入plist的格式,这里我以数组包字典的格式作为例子(格式可以自己选择,放发都是一样的)
写入plist
- 创建内层字典
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
- 将想要存入的信息添加到字典中(这里以字符串为例子)
[dictionary setObject:@"24K纯金洋" forKey:@"content"];
- 获取document文件夹的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [paths objectAtIndex:0];
- 设置plist的路径
NSString *plistPath = [documentDirectory stringByAppendingPathComponent:@"yang.plist"];
- 创建文件管理者
NSFileManager *fileManager = [[NSFileManager alloc] init];
- 写入本地
//查看路径内是否有plist文件//如果没有穿件一个//如果有执行写入操作if (![fileManager fileExistsAtPath:plistPath]) {//如果创建失败,提示用户 if (![fileManager createFileAtPath:plistPath contents:nil attributes:nil]) { NSAssert(NO, @"Failed to create file"); }else{ //如果成功,写入plist NSMutableArray *array = [NSMutableArray arrayWithObject:historyDic]; [array writeToFile:plistPath atomically:YES]; } }else{ //如果路径内有plist文件,先将数组取出,添加新内容,再写入 NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; [array addObject:historyDic]; [array writeToFile:plistPath atomically:YES]; }
取出plist
其实创建的时候已经有过取出的操作
NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentation = [path objectAtIndex:0]; [fileManager changeCurrentDirectoryPath:[documentation stringByExpandingTildeInPath]]; NSString *historyPlistPath = [documentation stringByAppendingPathComponent:@"history.plist"]; NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:historyPlistPath];
取出数组候可以对数组操作,一下内容都是基于取出plist的,所以下面的代码取出的部分就直接省略了
删除全部
if (array.count != 0) { if ([fileManager fileExistsAtPath:historyPlistPath]) { [array removeAllObjects]; [array writeToFile:historyPlistPath atomically:YES]; } }
- 额!不够完整,我的项目中先在就用到这几个方法,有时间继续!
0 0
- 数据持久化-写入plist
- 数据持久化-plist
- Plist--数据持久化1
- iOS 数据持久化 plist
- iOS 数据持久化-----plist
- iOS plist 数据持久化
- 数据持久化—plist
- swift数据持久化之属性列表,解决plist写入失败的问题
- 数据持久化之.plist文件
- 数据持久化之plist使用
- iOS 数据持久化之plist
- 数据持久化之属性列表Plist
- iOS 数据持久化 — Plist
- 数据持久化之.plist文件
- iOS数据持久化 归档 plist文件
- 数据持久化 plist,CoreData,Sqlite
- 数据持久化-plist文件的操作
- 数据持久化(NSUserDefault、Plist)
- HDFS冗余数据块的自动删除
- 从Spark 架构中透视Job
- POJ2828 Buy Tickets
- 查询每月的天数
- 三级联动数据
- 数据持久化-写入plist
- 344. Reverse String(将字符串倒序输出)
- 随时记录灵感——DP相关
- 快速排序的递归算法
- STL 优先队列
- Logistic回归
- SQLite数据库的增删改查
- 设计模式之单例模式
- The operation couldn’t be completed. (LaunchServicesError error 0.)