Which is the best way to handle creating an object to live in a retained property? I\'ve included several examples.
Assume the property is:
@property
If in initialization of the object which holds the variable:
1) no. it's bad form to call through accessors in partially constructed states (e.g. init, dealloc)
2) no. it's bad form to call through accessors in partially constructed states (e.g. init, dealloc)
3) correct.
Exception: If your ivars are not private and you are in the implementation of a subclass of the type which declared the property, then you must also check to see if the parent class initialized the property. it's best to make the properties private or otherwise not directly accessible to subclasses.
4) no. it's bad form to call through accessors in partially constructed states (e.g. init, dealloc)
When you're working with a fully constructed instance:
1) this is fine when readbaility is more important than keeping your heap sizes low.
2) bad. the object returned from the getter is not necessarily the object you assigned.
3) bad. may introduce a leak if _myProperty is not nil.
4) best