When is self needed for class properties? For example:
self.MyProperty = @\"hi there\";
vs
MyProperty = @\"hi there\";
This is an old question, though it used to be "When do I write [self setMyProperty:@"hi there"]
?" (Note that self.MyProperty = @"hi there"
is exactly equivalent to this.)
The answer I've always heard (and which makes good sense) is always use the accessor; never write MyProperty = @"hi there"
. There are several reasons:
MyProperty
needs to have a particular side effect, you can add to the setter method without finding every time you set MyProperty
.MyProperty
, it's easy to add logging code to the setter (or even getter) to find out every time it's changed (or even accessed).Summary: it's safest and most flexible to always use [self setMyProperty:@"hi there"]
or self.MyProperty = @"hi there"
, and never use MyProperty = @"hi there"
.