There are certain circumstances where it's generally discouraged to use the self.
-expression to access a property. Normally you always use self
for any access of a property. It's the most secure and uncomplicated way. Especially if you used retain, then memory management will be done for you.
The two exceptions from this rule:
- Any
init
method.
- In
dealloc
.
In both cases you are dealing with an partially initialized object. There are some side effects that may occur when using setters or getters here -- because they are methods and hence may be overridden.
For example, take a class A
with a property foo
that has been subclassed by class B
. The subclass B
adds an property bar
and overrode the setter for foo
. Now your init
-method calls setFoo:
, because you used self.foo = ...
with some initial value. The subclass, however, also accesses the value of bar
in this setter. But in this case, it may happen that bar has never been initialized and points at some arbitrary data. Calling a setter in init my cause crashes, although the probability may not be too high in your own code.