ios - Why I should access the instance variable directly from within an initialization method? -
the apple programming objective-c document states that:
you should access instance variables directly within initialization method because @ time property set, rest of object may not yet initialized. if don’t provide custom accessor methods or know of side effects within own class, future subclass may override behavior.
but don't know side effects in setter method, please give me example explain why have access instance variable directly within initialization method
the answer simple - code smell. dot notation self.foobar = something
in objective-c syntactic sugar messaging. sending messages self
fine. there 2 cases need avoid them:
1. when object being created, ,
2. when object being destroyed.
at these 2 times, object in strange in-between state. lacks integrity. calling methods during these times code smell because every method should maintain invariants operates on object.
Comments
Post a Comment