What is the Objective-C way of getting a nullable bool? What is the Objective-C way of getting a nullable bool? objective-c objective-c

What is the Objective-C way of getting a nullable bool?


An NSNumber instance might help. For example:

NSNumber *yesNoOrNil;yesNoOrNil = [NSNumber numberWithBool:YES]; // set to YESyesNoOrNil = [NSNumber numberWithBool:NO];  // set to NOyesNoOrNil = nil; // not set to YES or NO

In order to determine its value:

if (yesNoOrNil == nil){    NSLog (@"Value is missing!");}else if ([yesNoOrNil boolValue] == YES){    NSLog (@"Value is YES");}else if ([yesNoOrNil boolValue] == NO){    NSLog (@"Value is NO");}

On all Mac OS X platforms after 10.3 and all iPhone OS platforms, the -[NSNumber boolValue] method is guaranteed to return YES or NO.


I think you will need to use some class for that, e.g. wrap bool to NSNumber object.