将这三种方法合二为一。
- (void)updateEndDate:(NSDate *)end
startDate:(NSDate *)start
duration:(NSNumber *)duration
{
... insert your update logic here
[self willChangeValueForKey:@"startDate"];
[self willChangeValueForKey:@"endDate"];
[self willChangeValueForKey:@"duration"];
_endDate = end;
_startDate = start;
_duration = duration;
[self didChangeValueForKey:@"duration"];
[self didChangeValueForKey:@"endDate"];
[self didChangeValueForKey:@"startDate"];
}
在你的设置器中调用该方法
-(void)setStartDate:(NSDate *)start
{
[self updateEndDate:self.endDate startDate:start duration:self.duration];
}
KVO 合规性要求您指定手动发送属性通知
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey
{
BOOL automatic = NO;
NSArray * manualKeys = @[@"endDate", @"startDate", @"duration"];
if ([manualKeys containsObject:theKey])
{
automatic = NO;
}
else
{
automatic = [super automaticallyNotifiesObserversForKey:theKey];
}
return automatic;
}