iOS开发-plist文件增删改查

说到plist文件,很直接的想法就是把数据已经编译到手机上,非常的简单省时间。。。但是有个问题 我也是待验证   是不是代码操作可以删除目录下的plist文件    这个待验证哦!

plist第一次看到这个后缀名文件的时候感觉怪怪的,不过接触久了也就习以为常了,plist是Property List的简称可以理解成属性列表文件,主要用来存储串行化后的对象的文件。扩展名为.plist,因此被称为 plist文件,xCode中默认的是一种树状的结构展现出来数据,可视化的动态增删改查,非常人性化,不过最终的结果是以XML形式存储的,Plist文件可以用于存储用户的一些设置信息,具体根据需求而定。

简单创建文件

简单创建文件就是说可以直接从xCode创建,右击项目new File,可以添加一个plist文件:


创建一个UserData.plist文件,之后的内容如下:


右击open as->source code,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
         
        <key>Login</key>
        <dict>
            <key>UserName</key>
            <string>FlyElephant</string>
            <key>UserPassWord</key>
            <string>123456</string>
        </dict>
         
    </dict>
</plist>

读取设置的信息:

//读取Property List文件
NSString *userDataPath = [[NSBundle mainBundle] pathForResource:@"UserData" ofType:@"plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:userDataPath];
NSLog(@"%@",data);
NSLog(@"用户名:%@ 密码:%@", data[@"Login"][@"UserName"],data[@"Login"][@"UserPassWord"]);
[data setObject:@"登录信息" forKey:@"Login"];

增删改查

文件添加,上面是应用程序中添加文件,这个时候可以选择代码在沙盒中添加,代码如下:

NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取完整路径
NSString *documentsDirectory = [sandboxpath objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"sandbox.plist"];
//存储根数据
NSMutableDictionary *rootDic = [[NSMutableDictionary alloc ] init];
//字典中的详细数据
NSMutableDictionary *userDataDic = [[NSMutableDictionary alloc]init];
[userDataDic setObject:@"Flephant" forKey:@"UserName"];
[userDataDic setObject:@"http://www.cnblogs.com/xiaofeixiang/" forKey:@"UserPassWord"];
 
[rootDic setObject:userDataDic forKey:@"Login"];
//写入文件
[rootDic writeToFile:plistPath atomically:YES];
NSLog(@"%@",NSHomeDirectory());
NSLog(@"写入成功");
路径如下,具体路径获取上一篇文章已经可以看到:


读取数据:

//获取路径
NSArray *sandboxpath= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[sandboxpath objectAtIndex:0] stringByAppendingPathComponent:@"sandbox.plist"];
NSLog(@"%@",NSHomeDirectory());
//获取数据
NSMutableDictionary *searchdata = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",searchdata);
修改文件:
//获取路径
NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"sandbox.plist"];
//所有的数据列表
NSMutableDictionary *datalist= [[[NSMutableDictionary alloc]initWithContentsOfFile:filepath]mutableCopy];
 
//获取Login节点
NSMutableDictionary *loginData = [datalist objectForKey:@"Login"];
 
[loginData setValue: @"FlyElephant" forKey:@"UserName"];
[loginData setValue: @"123456" forKey:@"UserPassWord"];
[datalist setValue:loginData forKey:@"Login"];
[datalist writeToFile:filepath atomically:YES];
NSLog(@"修改成功");
  删除文件:
NSFileManager *manager=[NSFileManager defaultManager];
//文件路径
  NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"sandbox.plist"];
if ([manager removeItemAtPath:filepath error:nil]) {
    NSLog(@"文件删除成功");
}

作者:FlyElephant
出处: http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值