user230949
user230949

Reputation: 1119

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

Upvotes: 4

Views: 5738

Answers (2)

waterforest
waterforest

Reputation: 144

[NSNumber numberWithBool:yourBool]

If you do so, you will get a number when you call objectForKey@"boolKey". But if you add the bool for key in the plist file, you will get an NSCFBoolen.

Upvotes: 0

Thomas Zoechling
Thomas Zoechling

Reputation: 34253

You can wrap it in a NSNumber object:

  [NSNumber numberWithBool:yourBool]

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

Upvotes: 16

Related Questions