views:

205

answers:

1

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"];
+12  A: 

You can wrap it in a NSNumber object:

  [NSNumber numberWithBool:yourBool]

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

weichsel
Thanks. That does the correct conversion but the value still isn't saving into the plist. The path is correct. I'm running it in Xcode on the simulator. Do you have any ideas what could be wrong? It should throw an error if the path or key names are incorrect right?
Are you reading your plist into an `NSMutableDictionary`?
ohhorob
Yes - it is a NSMutableDictionary.