关闭

plist文件中的Boolean类型

185人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:74777次
    • 积分:1249
    • 等级:
    • 排名:千里之外
    • 原创:36篇
    • 转载:101篇
    • 译文:0篇
    • 评论:1条
    最新评论