For example?
- (void)setNow:(NSDate *)aDate {
[self willChangeValueForKey:@"now"];
_now = aDate;
[self didChangeValueForKey:@"now"];
}
Or directly
- (void)setNow:(NSDate *)aDate {
_now = aDate;
//自动调用 willChange didChange方法?
}
2 If you use runtime to add an attribute in a category, and if you need to use KVO, do you need to use these two methods manually?
-(void)setStr:(NSString *)str
{ //这里需要手动使用 willChange didChange么?
objc_setAssociatedObject(self, & strKey, str, OBJC_ASSOCIATION_COPY);
}
If the old value and the new value are different, you need to send a manual notification. Associated objects cannot synthesize setter methods, and setter methods must be given by the programmer. When we kvo this attribute, the runtime detects that we have implemented the setter and will not override this method, so the notification cannot be issued. So this notification also needs to be sent manually
It needs to be written manually, it will not be called automatically.