The first image is using self.name to change,and the second image using _name to change.it should be the same result,but the second one outputs nothing.why?
here is the code
#import "ViewController.h"
@interface kvo : NSObject
@property (nonatomic,strong) NSString *name;
@end
@implementation kvo
- (void)change
{
_name = @"b";
}
@end
@interface ViewController ()
@property (nonatomic, strong) kvo *a1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.a1 = [[kvo alloc] init];
_a1.name = @"a";
[self.a1 addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
[_a1 change];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"1");
}
the difference is self.name
and _name
in the change method
Edit:it's not the same question as "What's the difference between _variable & self.variable in Objective-C? [duplicate]", i know that's about the getter method and setter method,and my question is that why setter method fires the KVO and the _name = @"b"
does not fire the KVO.