Proper way to convert BOOL value into INT?
Solution 1
Maybe that will help(thought i don't know why it may be needed in the first place)
NSInteger i = @(YES).integerValue;
Hope that it helps you.
Solution 2
It is much quicker to do it like this:
BOOL myBool = YES;
int myInt = (myBool ? 1 : 0);
myInt
will be 1 if myBool
is YES, and 0 if myBool
is NO.
Solution 3
It is always possible to use the object-oriented wrapper around numeric primitives (e.g. C's int). So am I answering the question? both yes and no depending on your viewpoint. However, here is what I prefer to use when using BOOL as input:
NSNumber *numval = [NSNumber numberWithBool:Info.isattachementDownloadable];
And if you really need to use the primitive datatype:
int val = [numval intValue];
Solution 4
BOOL myBool;
int myInt;
if (myBool) myInt = 1;
else myInt = 0;
Solution 5
You can just assign BOOL value to int since YES and NO are just shortcuts for 1 and 0. You can also perform maths operations on BOOLs. FYI, BOOLs can even hold values from -128 up to 127 since they are just typedefs for signed chars.
BOOL someBool = YES;
int intFromBool = someBool;
NSLog(@"%d", intFromBool);
intFromBool -= YES;
NSLog(@"%d", intFromBool);
Reference: https://www.bignerdranch.com/blog/bools-sharp-corners/
Tariq
I am a software developer relentless in the pursuit of engineering elegance. I make it my goal to design technology with the human in mind, crafting a usable and intuitive user interface experience and highly readable and easily maintainable source code for efficient development. I am intensely passionate about, and skilled in, engineering Mac OS X, iPhone, and iPad applications using Cocoa and Objective-C. Contact Me: [email protected] @tariq2305 on Twitter.
Updated on January 15, 2020Comments
-
Tariq over 4 years
Everytime when I tried to convert BOOL value into int. INT value is showing -8 for True and 0 for False. Ideally it should return 1 for true.
I have tried all possible ways of conversion like
int val=Info.isattachementDownloadable; int val=[[NSString stringWithFormat:@"%d", Info.isattachementDownloadable] intValue]; int val=(int)Info.isattachementDownloadable;
where
Info.isattachementDownloadable
return BOOLIn all ways its showing -8 for TRUE.
Any suggestion ?
-
Tariq almost 13 yearsthis is pretty straightforward... but you are not doing any conversion ... is it conversion not possible ?
-
Tariq almost 13 yearsGreat that i was looking for :) ... i dont know why i missed this one :(
-
Blindy almost 13 yearsOr even better,
myBool!=0
, or JavaScript's patented!!myBool
. -
Alex Nichol almost 13 yearsI will point out that, for code that requires high efficiency, this is a poor method of converting a BOOL to an int because it allocates an object every time a conversion takes place.
-
Ariel over 12 yearsI agree, that it isn't fastest, nor memory friendly method, but it's objc straightest one that I know. Anyway, if you need efficiency - you can always fall back to plain c with
int myInt = (someBool ? 1 : 0);
as you've suggested in your answer. :) -
cncool over 9 years@AlexNichol [NSNumber numberWithBool:YES] returns a singleton. There is a maximum of one allocation no matter how many times you call that.
-
Albert Renshaw almost 8 yearsI may be wrong but don't BOOL's auto convert to this int value anyways in Obj-c when used as an int? For example NSLog(@"%i", YES+YES); logs 2
-
turingtested over 7 yearsThis can be needed when you for example want to use the tag-property on a UIButton as a BOOL - whether the button is recently pressed or not. Then you don't need to declare any extra property for this in the controller.