Proper way to convert BOOL value into INT?

24,673

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/

Share:
24,673
Tariq
Author by

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, 2020

Comments

  • Tariq
    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 BOOL

    In all ways its showing -8 for TRUE.

    Any suggestion ?

  • Tariq
    Tariq almost 13 years
    this is pretty straightforward... but you are not doing any conversion ... is it conversion not possible ?
  • Tariq
    Tariq almost 13 years
    Great that i was looking for :) ... i dont know why i missed this one :(
  • Blindy
    Blindy almost 13 years
    Or even better, myBool!=0, or JavaScript's patented !!myBool.
  • Alex Nichol
    Alex Nichol almost 13 years
    I 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
    Ariel over 12 years
    I 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
    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
    Albert Renshaw almost 8 years
    I 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
    turingtested over 7 years
    This 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.