plist文件中的Boolean类型

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类型,然后通过NSNumberboolValue方法来读取该值。例子如下:

    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];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值