I am aware that using setters in dealloc can create problems, if any other object is observing for changes in a property. But why should not we use them in initializers?
I have never had any problems using accessors in initializers. Maybe it depends on how much magic you have in the setters and getters – if the accessors did something too smart, you might run into trouble when using them in the initializer.
Now that I think of it, I even use the accessors in dealloc
. Again, without problems. If someone is observing an object, he should make sure that the object does not get deallocated in the first place.
So, unless somebody else comes up with a compelling counter-argument, I think you might try using the accessors both in init
and dealloc
and see how that works for you.
The only reason not to use accessors in -init
is because your object is not fully initialised and the accessor may depend on it. This is only likely to happen if you have a subclass that overrides the accessor methods.
There is a symmetrical problem on deallocation in that an overridden accessor might depend on not being called on a partially deallocated object. There is also the issue that you might send out spurious KVO notifications.
This goes to the heart of proper encapsulation. Subclasses should not need to care about the implementation details of the super class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With