Which should I use: self.property or _property
objective-c
Solution
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?
Problem
I'm sorry I do not know Objective-C well. Here is SomeClass.h : ``` @interface SomeClass : NSObject @property NSString *property; @end ``` We can use `property` in SomeClass.m as both `self.property` and `_property`. Which should I use? Is there some situation to decide which way to use? Thank you for your help.