Search code examples
iosobjective-cuiviewcontrollerivar

The best route to declare a BOOL as iVar or Property


I have read a few questions on the differences between iVars and Properties like these: Why would you use an ivar?
ios interface iVar vs Property

What I would like to know is... If I am creating a BOOL that needs to be accessed in multiple methods in a UIViewController (for example) what is the best way to create these?

At present I create proprties. This works fine and as expected. But as I read/learn more it appears that creating an iVar would be better for performance.

Like:

@interface ViewController : UIViewController{
BOOL myBool;
}

Would this be better for performance, and can multiple methods access this iVar if I set the value to YES in one, can I check the value in the other - as I can with property approach?


Solution

  • can multiple methods access this iVar if I set the value to YES in one, can I check the value in the other

    Of course you can, even if you set the value to NO. It is an instance variable and thus shared between all methods of one instance.

    Would this be better for performance

    No, unless you access the property very, very often, like 2^20 times per frame. Have a look at this Big Nerd Ranch post about iVar vs property performance. Usually the performance gain is not worth the loss in clarity.