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];
}
plist文件Boolean类型读写方法的更多相关文章
- php 获取文件mime类型的方法
php 获取文件mime类型的方法 使用fileinfo需要安装php_fileinfo扩展. 如已安装可以在extension_dir目录下找到php_fileinfo.dll(windows),f ...
- 还原TexturePacker plist 文件以及图片的方法 (切开各小图片)
原地址:http://blog.csdn.net/linuxchen/article/details/16865645 Python 脚本:(来自网络) unpack_plist.py 命令行: py ...
- plist文件真机写入方法
http://blog.csdn.net/mydo/article/details/50290219 转 但是这对真机不管用,因为在真机环境下,App在Xcode中的Resources文件夹都是不可 ...
- ios 简单的plist文件读写操作(Document和NSUserDefaults)
最近遇到ios上文件读写操作的有关知识,记录下来,以便以后查阅,同时分享与大家. 一,简单介绍一下常用的plist文件. 全名是:Property List,属性列表文件,它是一种用来存储串行化后的对 ...
- 使用ASP.NET 上传文件 三种类型判断方法(后缀,MIME,数据流)
#region 一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false; s ...
- ios本地文件内容读取,.json .plist 文件读写
ios本地文件内容读取,.json .plist 文件读写 本地文件.json .plist文件是较为常用的存储本地数据的文件,对这些文件的操作也是一种常用的基础. 本文同时提供初始化变量的比较标准的 ...
- C/C++读写文件的几种方法fstream fopen、fwrite()、fread()操作
C中采用的主要是文件指针的办法,C++中对文件的操作主要运用了"文件流"(即非标准的输入输出)的思想 c读写文件fopen C 库函数 FILE *fopen(const char ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- Dynamic CRM 2013学习笔记(十七)JS读写各种类型字段方法及技巧
我们经常要对表单里各种类型的字段进行读取或赋值,下面列出各种类型的读写方法及注意事项: 1. lookup 类型 清空值 var state = Xrm.Page.getAttribute(" ...
随机推荐
- WPFのInkCanvas作为蒙版透明笔迹不透明
本人最近利用inkcavas做一个蒙版的功能,结果发现笔迹稀释了,经过一番查找发现:应该讲inkcavas的背景设置为白色,然后透明,而不是将整个控件透明,具体代码: <InkCanvas Na ...
- Ajax jsonp
http://blog.csdn.net/superhosts/article/details/9057301
- 嵌入式之Linux系统裁剪和定制---(kernel+busyboxy+dropbear+nginx)
本文将介绍通过完全手动定制内核,在此基础上添加 busybox ,并实现远程登陆,使裁剪的 linux 能够运行 nginx . 在此之前介绍一下 linux 系统的启动流程. linux系统启动流程 ...
- stoneniqiu 理想就是自己喜欢做,并对社会和他人都有意义的事情!
H5嵌入原生开发小结----兼容安卓与ios的填坑之路 2016-11-19 08:29 by stoneniqiu, 1038 阅读, 8 评论, 收藏, 编辑 一开始听说开发H5,以为就是做适配现 ...
- [转](三)unity4.6Ugui中文教程文档-------概要-UGUI Basic Layout
大家好,我是孙广东. 转载请注明出处:http://write.blog.csdn.net/postedit/38922399 更全的内容请看我的游戏蛮牛地址:http://www.unityma ...
- mac 安装 nginx 环境
1.brew search nginx 2.brew install nginx 启动nginx ,sudo nginx ;访问localhost:8080 发现已出现nginx的欢迎页面了. 备注: ...
- NetBeans IDE配置调试
1.首先需要下载xdebug,在网址中http://www.xdebug.org/download.php可据实际配置环境下载,当然有的Apache安装后已经安装该项,就不必作处理: 2.配置php. ...
- android sdk 更新那些文件
上篇经验,完成了android开发环境的搭建,相信大家也下载了那1.52G,已经下载好了的Adt_bundle. 那么,我们来点击SDK Manager.exe,看看有些什么吧 2 如图所示,为整个目 ...
- go文件操作大全
参考Go官方库的文件操作分散在多个包中,比如os.ioutil包,我本来想写一篇总结性的Go文件操作的文章,却发现已经有人2015年已经写了一篇这样的文章,写的非常好,所以我翻译成了中文,强烈推荐你阅 ...
- json对象,数组,字符串总结
关于json对象,数组,字符串的总结 什么是json? JSON(JavaScript Object Notation) 一种轻量级的数据交换格式,JSON采用完全独立于语言的文本格式...(来自百 ...