简单types(非NSObject)上的KVO(IOS 5)

我有问题,使IOS(Objective-C)KVO工作的键typesint。

我的类声明了一个inttypes的属性sampleValue 。 由于int不会自动实现KVOfunction,我已经自动忽略了方法NotNotifyObserversforKey:

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey { BOOL automatic = NO; if ([theKey isEqualToString:@"sampleValue"]) { automatic = NO; } else { automatic=[super automaticallyNotifiesObserversForKey:theKey]; } return automatic; } 

这个方法就像我期望的那样被调用。 我也已经实现了像这样的sampleValue属性的setter方法:

 - (void) setSampleValue:(int)newSampleValue { [self willChangeValueForKey:@"sampleValue"]; sampleValue = newSampleValue; [self didChangeValueForKey:@"sampleValue"]; } 

在观察者类中设置观察者是这样完成的(dc是观察对象的实例):

 [dc addObserver:self forKeyPath:@"sampleValue" options:NSKeyValueObservingOptionNew context:NULL]; 

但是,更新sampleValue时,不会向我的观察者对象发送通知。 更新NSDatetypes的另一个属性绝对没问题。

任何人都可以帮我弄清楚我做错了什么,或者我应该怎么做才能做到这一点。

最好的问候Tomo

也许我在你的问题中缺less了一些东西,但是你可以像其他types一样容易地观察inttypes的属性,而不需要做任何特殊的事情。

尝试删除您的+automaticallyNotifiesObserversForKey: -setSampleValue: +automaticallyNotifiesObserversForKey:覆盖和您的-setSampleValue:设置,并只是综合sampleValue的访问sampleValue

 @synthesize sampleValue; 

int是与key @"sampleValue"对应的值的types,但不是被观察的东西。 被观察的对象是dc ,当sampleValue属性发生变化时, sampleValue负责发送正确的通知。