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.
bbum
source share