Should I use ivars in Objective-C?

I have an application that I am writing that uses only @properties. I don't have any ivar declared at all in any of my class files. As far as I understand, ivars are no longer needed with the introduction of @property. Am I coding according to best practice? Does this ultimately bite me in the notorious butt in the long run? I read mixed reviews about what's “right” and “wrong” ...

+7
source share
4 answers

I don’t declare Ivars at all. I often use @synthesize foo = foo_; although to prevent direct access, when I meant the pass-through method or vice versa. And I always let the compiler automatically synthesize ivar with the _ prefix (which prevents accidental direct access, according to the phrase struck ).

And, as Caleb said, there are still Ivars floating around, you just don’t explicitly declare them unless you really want to (which is really not how the exposed ivars in the headers are not useful for class clients, if your API designed accordingly).

I also believe that the hype about "uses only direct access to init / dealloc, uses setter / getter everywhere" to be pretty bloated and thus just use the setter / receiver everywhere. The reality is that if you have observers during initialization / release, you were already busy; the state of the object is, by definition, undefined during construction / demolition, and thus the observer cannot reason about the state correctly.


As Caleb points out, another reason for using ivar direct access in init / dealloc is to avoid subclasses that implement the setter / getter user logic, which may be disabled due to the undefined state of the object during init / dealloc.

Although this may be true, I find this an unpleasant architectural flaw for implementing setters / getters with custom behavior. This is unstable and greatly complicates the reorganization of code over time. In addition, this usual behavior will often depend on a different state inside the object, and this dependence then leads to dependencies of orders on state changes that are not reflected in the seemingly simple @property declaration.

those. if your setters and getters are written so that foo.bar = bad; cannot be executed at any time on foo , then your code will be blocked.

+11
source

Not so much that instance variables are not needed. It's just that instance variable declarations are not needed. Given the property and the @synthesize operator, the compiler will take care of creating the instance variable along with the appropriate access methods.

There is nothing wrong with using properties exclusively. They simplify memory management. There's also nothing wrong with using iVars without properties, if that is what you want. If you want to use properties but don’t want to advertise accessors in the rest of the world (i.e., support encapsulation), consider declaring your non-public properties in a class extension (basically an anonymous category in your implementation file).

+15
source

Using iVars is certainly not the case, but the best methods now use to use @property.

+2
source

One place where you can use ivar is when you want to declare a protected property. You declare a property in the .m file for the class and declare its corresponding ivar in .h using the @protected directive. This will allow you to get secure access in a subclass. There is no alternative to secure member access.

0
source

All Articles