Objective-C KVO Should I manually implement willchange/didChange in the set method?
漂亮男人
漂亮男人 2017-05-02 09:27:56
0
2
524

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);  
}  
漂亮男人
漂亮男人

reply all(2)
我想大声告诉你

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template