I understand the difference between the two: self.propertyName uses the accessor method whereas _propertyName accesses the instance variable directly. So as I understand it, the only time you don’t want to use the self.propertyName is inside the property’s accessor method, since it would cause an infinite loop. As long as I follow that rule, is it ok, and perhaps good for the sake of consistency, to use self.propertyName every time you need to get/set that property inside a class method?
Thanks so much for your wisdom!
As well as setters and getters, you should avoid dot notation in your
initanddeallocmethods on the grounds that it’s not safe to call published methods on a class that’s half constructed or half destroyed.The particular risk is that a subclass might have added logic into its accessors. If you call the accessor in
deallocthen you’ll call the subclass after it’s deallocated itself. If you call the accessor ininitthen you’ll call into the subclass before it’s initialsed itself.Much the same thing can also happen if you have any key-value observers — of you call a setter then they’re alerted of the change and given a reference to you regardless of your current state. So it’s not even safe if you strictly disallow subclassing.