iOS中plist文件操作
2016-02-17 13:42
537 查看
1、先创建一个plist文件在项目中:
new file - Resource - Property plist - Next(下一步) - 更改名称(chatUserName)- 确认
这样就创建好了一个plist文件
2、获取plist文件路径:
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"chatNiceName"
ofType:@"plist"];
3、往plist文件中存入数据:(我这里用的是字典形式,你可以根据需要选择是字典还是数组)
/**
* 往plist文件中写入信息,text作为字典的key 与 value
*/
-(void)writeMsgToPlistFile:(NSString *)text
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0]
stringByAppendingPathComponent:@"chatNiceName.plist"];
NSMutableDictionary *dataList = [[[NSMutableDictionary
alloc]
initWithContentsOfFile:path]
mutableCopy];
NSMutableDictionary *dict1 = [NSMutableDictionary
dictionary];
[dict1 setObject:text
forKey:text];
[dataList setObject:dict1
forKey:text];
[dataList writeToFile:path
atomically:YES];
}
plist存入的信息图片展示:
4、读取刚才存在字典里面的信息
/**
* 读取plist文件中写入信息,text作为字典的key 与 value
*/
-(void)getPlistFileAllMsg:(NSString*)text
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0]
stringByAppendingPathComponent:@"chatNiceName.plist"];
NSMutableDictionary *dataList = [[[NSMutableDictionary
alloc]
initWithContentsOfFile:path]
mutableCopy];
NSMutableDictionary *dict = [dataList
objectForKey:text];
NSLog(@"%@", dict);
}
打印出来的报文信息
问题:
在论坛上看到有人说往plist文件存入字典信息,会把之前的dict给覆盖掉。这里复原下这种情况代码
如果你取出plist的代码是这样的,那么将会出现覆盖的情况
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"chatNiceName"
ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
new file - Resource - Property plist - Next(下一步) - 更改名称(chatUserName)- 确认
这样就创建好了一个plist文件
2、获取plist文件路径:
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"chatNiceName"
ofType:@"plist"];
3、往plist文件中存入数据:(我这里用的是字典形式,你可以根据需要选择是字典还是数组)
/**
* 往plist文件中写入信息,text作为字典的key 与 value
*/
-(void)writeMsgToPlistFile:(NSString *)text
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0]
stringByAppendingPathComponent:@"chatNiceName.plist"];
NSMutableDictionary *dataList = [[[NSMutableDictionary
alloc]
initWithContentsOfFile:path]
mutableCopy];
NSMutableDictionary *dict1 = [NSMutableDictionary
dictionary];
[dict1 setObject:text
forKey:text];
[dataList setObject:dict1
forKey:text];
[dataList writeToFile:path
atomically:YES];
}
plist存入的信息图片展示:
4、读取刚才存在字典里面的信息
/**
* 读取plist文件中写入信息,text作为字典的key 与 value
*/
-(void)getPlistFileAllMsg:(NSString*)text
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0]
stringByAppendingPathComponent:@"chatNiceName.plist"];
NSMutableDictionary *dataList = [[[NSMutableDictionary
alloc]
initWithContentsOfFile:path]
mutableCopy];
NSMutableDictionary *dict = [dataList
objectForKey:text];
NSLog(@"%@", dict);
}
打印出来的报文信息
问题:
在论坛上看到有人说往plist文件存入字典信息,会把之前的dict给覆盖掉。这里复原下这种情况代码
如果你取出plist的代码是这样的,那么将会出现覆盖的情况
NSString *plistPath = [[NSBundle
mainBundle] pathForResource:@"chatNiceName"
ofType:@"plist"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
相关文章推荐
- iOS开发各种证书问题
- iOS 设计模式之装饰模式(Decorator)
- iOS开源库
- AFNetworking3.0后容易出现的问题
- 真机调试时出现"The identity used to sign the executable is no longer valid"
- iOS获取当前时间及前一天或后一天时间
- iOS 手势
- 学习IOS开发的顾虑
- 关于iOS六大基本设计模式
- XCode6 设置Icon
- iOS 证书失效
- iOS-OC-监听蓝牙是否开启(打开状态)
- IOS跑马灯效果
- 培训机构出来的iOS学员怎么了?
- tabbar ios
- iOS关于启动页自定义特殊处理
- iOS开发——keychain的使用
- iOS上获取大文件md5的方法
- ios全部证书失效
- GitHub上值得关注的iOS开源项目