There are two parts to your question.
Part 1:
Using self.myProp = xyz
calls the setter of the property to set the data, instead of direct assigning myProp = xyz
. It's advised to access and modify data, even private or internal data, by setters and getters. They give you a way of handling related issues with the data as @SplinterReality mentioned in his answer. But the related issues can be more than that, for example:
Lazy loading
- (SomeObject *) myProp {
if(_myprop == nil) {
_myProp = [[SomeObject alloc] init];
}
return _myProp;
}
Doing some related actions Note: This can also be handled by KVO. This is simpler.
- (void) setProperty:(SomeObject *)object (
_property = object;
id relatedProperty = //Contruct related value here.
self.relatedProperty = relatedProperty;
[self updateViewThatShowsPropertyContent];
}
Part 2:
Using @synthesize
is used to create the default setters and getters of the property, i.e. you're not going to provide your custom implementation for them.
@synthesize myProp = variableName;
makes the generated instance variable named _variableName
. But the default behavior is generating an instance variable called _property
for @property property
. So @synthesize myProp = _myProp;
and @synthesize myProp;
aren't different.
However, in modern Objective-C used right now, @synthezie
is not needed, i.e. it's called for you. So unless you need to provide an instance variable other than the default name, you don't need to call it.