i have a question about using getters and instance variables. Let’s see an example.
Suppose i have in a .h file:
@property (nonatomic,strong) NSString *name
and in the .m file i synthesize that variable in this way:
@synthesize name = _name;
Now my question is: what’s the difference between use:
[self.name aMethod]
and
[_name aMethod]
Thanks!
The first one accesses the ivar through the getter method. The second directly accesses the ivar. Since it’s a simple, synthesized property, there’s not much difference except that the first makes an additional method call. However, if the property were atomic, or dynamic, or the getter method were complicated, there’d be a difference in that the first one would actually be atomic while the second wouldn’t and the first would actually trigger any complicated logic in the getter while the second wouldn’t.
In simplest terms, the compiler re-writes the first call to:
while the second call is simply left as-is.