简单类型(非 NSObject)上的 KVO (IOS 5)
我在使 IOS (objective-c) KVO 为 int 类型的键工作时遇到问题。
我的类声明了一个 int 类型的属性 sampleValue。由于 int 不会自动实现 KVO 功能,因此我重写了方法 automaticallyNotizesObserversforKey ,如下所示:
+ (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更新时,不会发送任何通知我的观察者对象。更新 NSDate 类型的另一个属性绝对没问题。
任何人都可以帮助我弄清楚我做错了什么或者我应该做什么才能使这项工作成功。
此致 友
I'm having problems to make the IOS (objective-c) KVO work for a key of type int.
My class declares a property sampleValue of type int. As int doesn't automatically implement the KVO functionality I've overrided the method automaticallyNotifiesObserversforKey as this:
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey {
BOOL automatic = NO;
if ([theKey isEqualToString:@"sampleValue"]) {
automatic = NO;
} else {
automatic=[super automaticallyNotifiesObserversForKey:theKey];
}
return automatic;
}
The method is called just as I would expect is to be. I also have implemented a setter method for the sampleValue property like this:
- (void) setSampleValue:(int)newSampleValue
{
[self willChangeValueForKey:@"sampleValue"];
sampleValue = newSampleValue;
[self didChangeValueForKey:@"sampleValue"];
}
Setting up the observer in the observer class is done like this (dc is the instance of the observed object):
[dc addObserver:self forKeyPath:@"sampleValue" options:NSKeyValueObservingOptionNew context:NULL];
However, when the sampleValue is updated, no notification is sent to my observer object. Updating another property of type NSDate works absolutely fine.
Can anyone help me figure out what I'm doing wrong or what I should do to make this work.
Best regards
Tomo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许我在您的问题中遗漏了一些内容,但是您可以像其他类型一样轻松地观察
int
类型的属性,而无需执行任何特殊操作。尝试删除您的
+automaticallyNotizesObserversForKey:
覆盖和-setSampleValue:
设置器,然后合成sampleValue
的访问器:int
是与键@"sampleValue"
对应的值的类型,但它不是被观察的事物。被观察的对象是dc
,它会在sampleValue
属性发生更改时发送适当的通知。Maybe I'm missing something in your question, but you can observe properties of type
int
just as easily as other types without doing anything special.Try removing your
+automaticallyNotifiesObserversForKey:
override and your-setSampleValue:
setter, and just synthesize the accessors forsampleValue
:int
is the type of the value that corresponds to key@"sampleValue"
, but it's not the thing being observed. The object being observed isdc
, and it'll take care of sending the proper notification when thesampleValue
property is changed.