简单类型(非NSObject)上的KVO(IOS 5) [英] KVO (IOS 5) on simple types (non NSObject)

查看:83
本文介绍了简单类型(非NSObject)上的KVO(IOS 5)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使 IOS(objective-c)KVO 用于int类型的键方面遇到问题.

我的课程声明了一个类型为int的属性 sampleValue .由于int不会自动实现KVO功能,因此我已经覆盖了 automaticallyNotificationObserversforKey 方法,如下所示:

+ (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类型的另一个属性绝对可以.

任何人都可以帮助我弄清楚我做错了什么或应该做些什么来完成这项工作.

最诚挚的问候 托莫

解决方案

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

尝试删除您的+automaticallyNotifiesObserversForKey:替代项和-setSampleValue:设置器,然后仅合成sampleValue的访问器:

@synthesize sampleValue;

int是与键@"sampleValue"对应的值的类型,但不是正在观察的东西.被观察的对象是dc,并且当sampleValue属性更改时,它将负责发送适当的通知.

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

解决方案

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.

这篇关于简单类型(非NSObject)上的KVO(IOS 5)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆