convert NSNumber to BOOL objective C

ios, ipad, iphone, objective-c

Solution

First of all, the initialization of your `NSNumber` is incorrect. You should use one of the `+numberWith:` class methods defined on `NSNumber`:

+ (NSNumber *)numberWithChar:(char)value;
+ (NSNumber *)numberWithUnsignedChar:(unsigned char)value;
+ (NSNumber *)numberWithShort:(short)value;
+ (NSNumber *)numberWithUnsignedShort:(unsigned short)value;
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithUnsignedInt:(unsigned int)value;
+ (NSNumber *)numberWithLong:(long)value;
+ (NSNumber *)numberWithUnsignedLong:(unsigned long)value;
+ (NSNumber *)numberWithLongLong:(long long)value;
+ (NSNumber *)numberWithUnsignedLongLong:(unsigned long long)value;
+ (NSNumber *)numberWithFloat:(float)value;
+ (NSNumber *)numberWithDouble:(double)value;
+ (NSNumber *)numberWithBool:(BOOL)value;
+ (NSNumber *)numberWithInteger:(NSInteger)value NS_AVAILABLE(10_5, 2_0);
+ (NSNumber *)numberWithUnsignedInteger:(NSUInteger)value NS_AVAILABLE(10_5, 2_0);

`BOOL`s are just `signed char`s, so you cannot use the `%@` format specifier, but you can use any of the integral format specifiers such as `%d`, `%i` or `%c`.

However, to output `YES` or `NO`, you'd need to use a string:

NSLog(@"bool is: %@", (myBool) ? @"YES" : @"NO");

Problem

I want to convert integers `0` and `1` to BOOLEAN `YES` and `NO` My code: ``` NSNumber *num = 0; BOOL myBool = [num boolValue]; NSLog(@"bool is: %@",myBool); ``` It gives output as `(null)` What could be wrong?

Original source