Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert NSNumber to BOOL objective C

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?

like image 728
Eman87 Avatar asked Apr 26 '12 21:04

Eman87


4 Answers

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);

BOOLs are just signed chars, 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");
like image 177
Jacob Relkin Avatar answered Oct 15 '22 11:10

Jacob Relkin


Given the incorrect assignment to NSNumber did you really mean to create an object containing an integer, or is your title question "convert int to BOOL" correct?

If the latter then to convert an int with the value 0 or 1 to BOOL you just cast:

int num = ...; // num is 0 or 1
BOOL myBool = (BOOL)num; // myBool is NO or YES

However a better conversion is:

BOOL myBool = num != 0; // myBool is NO for 0, YES for anything else

as this follows the Obj-C (and C) notion that zero is false and non-zero true.

If you wish to convert a BOOL value to the strings "YES" and "NO" then this simple function will do it efficiently:

NS_INLINE NSString *BoolToStr(BOOL b) { return b ? @"YES" : @"NO"; }

just place it in a convenient header and import wherever you need it.

like image 28
CRD Avatar answered Oct 15 '22 13:10

CRD


To Covert Int to BOOL

convert an int with the value 0 or 1 to BOOL you just cast

NSNumber *num = [NSNumber numberWithInt:0]; // num is 0 or 1
BOOL myBool = [num boolValue];  // myBool is NO or YES
like image 4
Nikhlesh Bagdiya Avatar answered Oct 15 '22 12:10

Nikhlesh Bagdiya


As sch mentioned you messed it up right here:

NSNumber *num = 0;
  • asterisk is a pointer/reference to an object ( a block of memory in your RAM ). It can hold just the memory address value ( no integers/bools ) of memory block for your object. Even in Objective-C don't mistake BOOL, int, float, double, NSInteger numeric types with objects (NSNumber).
like image 2
deathhorse Avatar answered Oct 15 '22 11:10

deathhorse