简单类型(非 NSObject)上的 KVO (IOS 5)

发布于 2024-12-27 09:03:25 字数 1077 浏览 1 评论 0原文

我在使 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2025-01-03 09:03:25

也许我在您的问题中遗漏了一些内容,但是您可以像其他类型一样轻松地观察 int 类型的属性,而无需执行任何特殊操作。

尝试删除您的 +automaticallyNotizesObserversForKey: 覆盖和 -setSampleValue: 设置器,然后合成 sampleValue 的访问器:

@synthesize 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 for sampleValue:

@synthesize sampleValue;

int is the type of the value that corresponds to key @"sampleValue", but it's not the thing being observed. The object being observed is dc, and it'll take care of sending the proper notification when the sampleValue property is changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文