@property (nonatomic, copy ) NSString *title;
@synthesize title = _title;
what does this '_title' mean?
Any help appreciated.Thanks in advance
@property (nonatomic, copy ) NSString *title;
@synthesize title = _title;
what does this '_title' mean?
Any help appreciated.Thanks in advance
_title
is iVar. mean it is variable and title just is a property.You are synthesizing by assigning iVar to property.Because you need not write the methods like
- (NSString *)title {
return _title;
}
- (void)settitke:(int)newValue {
_title = newValue;
}
Those are getter and setters
when you do
title =_title
.Those two above methods will abstract the setter and getter in one line.
It means that the backing iVar for the property has a leading underscore.
So you can use property syntax to access it:
self.title = @"Some title";
or you access it directly, but using the underscore name
_title = @"Some title";
Although using direct access is discouraged except for initialisers and the dealloc method.
If you are using auto synthesis, the underscore is added by default.
The reason for this:
It makes it a lot clearer when you are using direct access to refer to an iVar rather than a local variable, or a property.
It also means that you can use obvious names for method parameters and not worry about them clashing; for example, if you were to write a method that referred to the property: