plist文件Boolean类型读写方法
http://blog.csdn.net/a6472953/article/details/7659505 转
1、读取plist文件中的Boolean类型的字段值时,要先把它转为NSNumber类型,然后通过NSNumber的boolValue方法来读取该值。例子如下:
bool IsTrue=[(NSNumber*)[dic objectForKey:@"IsTrue"]boolValue];
2、写入时也是类似:
Boolean setting =NO;
NSNumber *testBoolean =[[NSNumber alloc]initWithBool:setting];
然后,才进行 plist文件的读写
3、读写plist文件
//下面函数主要是 获取的UISwitch(即switchView,在IB中进行了关联)的值,将其当前的值保存到plist文件中,以便程序下次启动时使用;通过这种方式可以保存和读取程序的一些配 置信息
- (void)viewDidLoad
{
[superviewDidLoad];
#if 0
//1、创建plist文件
//获取沙盒路径,创建plist文件,因为系统的list文件是只读属性,在沙盒中的文件才是可读和可写的,必须在沙盒中创建plist文件
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *filename=[path stringByAppendingPathComponent:@"personal.plist"];
//创建一个NSDictionary
NSMutableDictionary *dictionary =[[NSMutableDictionary alloc]init];
//创建3个添加到dictionary中的变量,并对其赋值
NSString *testString = [[NSString alloc]initWithString:@"fistValue1111"];
NSNumber *testInt = [[NSNumber alloc]initWithInt:5];
NSNumber *testBoolean =[[NSNumber alloc]initWithBool:YES];
//将3个变量添加到dictionary中
[dictionary setValue:testString forKey:@"String test"];
[dictionary setValue:testInt forKey:@"INteger test1"];
[dictionary setValue:testBoolean forKey:@"Boolean test"];
//将dictionary中的数据写入plist文件中
[dictionary writeToFile:filename atomically:YES];
NSLog(@"%@",filename);
#endif
/******************************************************************/
//2、读取plist文件*获取某一个key的对应的valuse
//读取plist文件,获取UISwitch的值,根据值来设置UISwitvch的显示
//获取沙盒路径,创建plist文件,因为系统的list文件是只读属性,在沙盒中的文件才是可读和可写的,必须在沙盒中创建plist文件
NSArray *readPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *readPath=[readPaths objectAtIndex:0];
NSString *plistPath=[readPathstringByAppendingPathComponent:@"personal.plist"];
//读取到一个NSDictionary
NSDictionary *dictionary1 = [[NSDictionaryalloc]initWithContentsOfFile:plistPath];
//读取plist文件中的Boolean类型的字段值时,要先把它转为NSNumber类型,然后通过NSNumber的boolValue方法来读取该值。例子如下:
bool switchFlag=[(NSNumber*)[dictionary1objectForKey:@"Boolean test"]boolValue];
//Boolean switchFlag = [dictionary1 objectForKey:@"Boolean test"];
[self.switchViewaddTarget:selfaction:@selector(switchViewChange:)forControlEvents:UIControlEventValueChanged];
if(switchFlag)
{
NSLog(@"switch的值为 NO");
switchView.on = YES;
//switchView.on
}
else//switchFlag=NO
{
NSLog(@"switch的值为 YES");
switchView.on = NO;
}
//读取到一个NSArray
// NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)switchViewChange:(id)sender
{
UISwitch *theSwitch =(UISwitch *)sender;
NSLog(@"switch do nothing");
Boolean setting = theSwitch.on;
if(setting == NO)
{
NSLog(@"current setting = NO");
}
else if(setting == YES)
{
NSLog(@"current setting = YES");
}
//获取沙盒路径,创建plist文件,因为系统的list文件是只读属性,在沙盒中的文件才是可读和可写的,必须在沙盒中创建plist文件
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path=[paths objectAtIndex:0];
NSString *plistPath=[pathstringByAppendingPathComponent:@"personal.plist"];
NSMutableDictionary *dictionary =[[NSMutableDictionaryalloc]initWithContentsOfFile:plistPath];
NSNumber *testBoolean =[[NSNumber alloc]initWithBool:setting];
[dictionary setValue:testBoolean forKey:@"Boolean test"];
[dictionary writeToFile:plistPath atomically:YES];
}
最新文章
- Android中Context的理解及使用(一)——Context的作用
- android SDK manager 无法获取更新版本列表
- node,不懂不懂
- ajax简单案例:字符串返回类型
- PHP之session_start()详解
- 国外.net学习资源网站
- 原生js怎么为动态生成的标签添加各种事件
- oracle 序列介绍
- BZOJ:4219: 跑得比谁都快 3007: 拯救小云公主
- php扩展之Yar
- 高手进阶,终极内存技术指南——完整/进阶版 II (转)【转】
- 用layui搭建的后台框架
- mysql查询前几条记录
- c# sqlite 数据库加密
- MSSQL 2012 修改所有表的架构Schame
- 【转】OPPO A77保持应用后台运行方法
- 2018SDIBT_国庆个人第四场
- centos 6.9 +nginx 配置GIT HTTPS服务器(证书采用自签名)
- underscore.js源码研究(2)
- 阿里云数据库配置学习笔记(二):下载并配置MySQL数据库
热门文章
- windows 8.1无人值守安装
- flex垂直居中
- 在用busybox制作系统过程中遇到的问题
- wifipineapple使用教程
- Python namedtuple
- 简单说一下printf(";%*s%s";,xx,xx,xx);或printf(";%*s\n";,xx,xx);
- 如何将php的错误输出到nginx的error_log中去
- poj2778DNA Sequence(AC自动机+矩阵乘法)
- AVKit &; MediaPlayer简写
- 对数组进行malloc动态分配的一些总结