1

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.

ios_Dev
  • 95
  • 1
  • 10

1 Answers1

1

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
  • Thankyou what is the difference between assigning true false & YES NO ? – ios_Dev Dec 20 '16 at 09:34
  • **what are you doing having instance variables in an Objective-C class that don't start with an underscore** Sir i am kind of new to this , can you please explain why the instance variables should start with underscore (i.e. properties i guess) Assigning false to a BOOL* works because this [http://stackoverflow.com/questions/541289/objective-c-bool-vs-bool] – ios_Dev Dec 20 '16 at 09:43
  • YES and NO are Objective C coding conventions. Instance variables starting with underscore are Objective C coding conventions. That way a reader sees immediately that you are using an instance variable, and not a local variable, parameter, global variable, extern global variable, enum, or whatever, but an instance variable. Using properties will save your life at some point. – gnasher729 Dec 20 '16 at 09:59
  • i was told to use properties , when i need to access it from another class, should i always be using properties ? – ios_Dev Dec 20 '16 at 10:04