Typically in standard practice, you only ever use _property
in a getter/setter/init/dealloc method. In all other cases you use self.property
or [self property]
Why?
Using _property
is strictly used to assign or get a value directly, while using self.property is the same as calling [self property].
In this way, you can create custom getters/setters for the method that all classes are required to abide by when they use this variable, including the class holding the variable itself.
For example:
If I call object.property
, I am essentially calling [object property]
. Now this doesn't matter so much if you don't define custom methods, but if you have a custom getter or setter method in your object's class:
- (Type)property{
return 2*_property;
}
//AND/OR
- (void)setProperty:(Type)property
{
_property = 2*property;
}
Then there will be a difference between using _property
and self.property
.
Make sense?