iOS开发-plist文件增删改查 – Fly_Elephant

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(@”文件删除成功”);
}

  

 

本文链接:iOS开发-plist文件增删改查,转载请注明。



You must enable javascript to see captcha here!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress

无觅相关文章插件,快速提升流量