ios_Dev
ios_Dev

Reputation: 95

Why i can not assign true to a BOOL in objective C?

Created a BOOL *myBool variable in implementation file as below:

@interface myClass ()

{
    BOOL *myBool;
}

- (void)viewDidLoad {        
    [super viewDidLoad];

    myBool = false; // no error
 }
- (IBAction)myBtnClickd:(UIButton *)sender {                
    if (!myBool) { 

        myBool = true; //error: incompatible integer to pointer conversion assigning to BOOL from int.            
    }        
    else { 

        myBool = false;                         
    }                
}

why I can not assign true to it and I am not assigning any int as we can see in the code, I do not want to make it a property.

Upvotes: 1

Views: 1172

Answers (1)

gnasher729
gnasher729

Reputation: 52530

You can't assign true to BOOL, because you are not trying to assign to BOOL, but to BOOL*. A pointer to BOOL. Assigning false to a BOOL* works for some weird reasons deeply hidden in the C standard.

Anyway, this is Objective-C. Why are you using true and false in Objective-C? It's either YES or NO.

Anyway, what is that nonsense code? Just write myBool = ! myBool.

Anyway, what are you doing having instance variables in an Objective-C class that don't start with an underscore, and why are you not using properties? That code should be either

self.myBool = ! self.myBool;

or

_myBool = ! _myBool;

And of course the BOOL* should be a BOOL. BOOL is not a reference type, it's a value type.

Upvotes: 1

Related Questions