Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a BOOL to plist?

I'd like to assign a boolean value to a plist file entry. I'm doing the following:

NSString *aBool = realBoolValue ? @"YES" : @"NO";      
[myplist setValue: aBool forKey:@"boolKey"];
[myplist writeToFile: [NSHomeDirectory() stringByAppendingPathComponent: plistFilePath] atomically:NO];

But the assignment never takes. I'm doing the above because the following doesn't work:

[myplist setValue: realBoolValue forKey:@"boolKey"];

It gives an incompatible type error. What am I doing wrong?

-- EDIT ---

plistFilePath is initialized as

plistFilePath = [NSHomeDirectory() stringByAppendingPathComponent: @"Library/Preferences/myfile.plist"];
like image 605
user230949 Avatar asked Dec 06 '22 04:12

user230949


1 Answers

You can wrap it in a NSNumber object:

  [NSNumber numberWithBool:yourBool]

Use [NSNumber boolValue] when reading back the value from the plist.

like image 83
Thomas Zoechling Avatar answered Dec 18 '22 01:12

Thomas Zoechling