Inside an objective class, there are 2 ways to access any declared property
self.propertyName_propertyNameAlthough both the uses hypothetically have the similar effect, there are 3 distinct differences
self.propertyNametrigggers the setter, which is important if you are performing any additional operations or validations. This does not happen when accessing the ivar directlyself.propertyNamealso triggers KVO Notifications which _propertyName conveniently ignores.Accesing property also gives access to various memory management features for free. Eg: a property with
copyattribute will enforce it by creating a copy of the instance being assigned rather than just assigning the pointer, which is particularly useful when assinging mutable data to immutable property type.Example:
@interface ViewController ()
@property (nonatomic, strong) NSString *theName;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[self addObserver:self
forKeyPath:@"theName"
options:NSKeyValueObservingOptionNew
context:nil];
self.theName = @"Hello world"; // Triggers KVO
_theName = @"Fancy world"; // No KVO
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSLog(@"Key path changed - %@", change);
}
output
Notice only one of the 2 assignments triggers KVO notification
2014-06-13 06:03:56.163 NotifTest[24710:1356234] Key path changed - {
kind = 1;
new = "Hello world";
}