Restoring a BOOL inside an NSDictionary from a plist file

William Jockusch picture William Jockusch · Sep 29, 2010 · Viewed 19.6k times · Source

I have a plist file which contains an array of dictionaries. Here is one of them:

Fred Dictionary
Name Fred
isMale [box is checked]

So now I am initializing my Person object with the dictionary I read from the plist file:

 -(id) initWithDictionary: (NSDictionary *) dictionary {
    if (self = [super init])
    self.name = [dictionary valueForKey: @"Name"];
    self.isMale = ????
  }

How do I finish off the above code so that self.isMale is set to YES if the box is checked in the plist file, and NO if it isn't. Preferably also set to NO if there is no key isMale in the dictionary.

Answer

Vladimir picture Vladimir · Sep 29, 2010

BOOL values usually stored in obj-c containers wrapped in NSNumber object. If it is so in your case then you can retrieve bool value using:

self.isMale = [[dictionary objectForKey:@"isMale"] boolValue];