开发者

KVO - Incompatible pointer types in Cocoa callback 'Class' vs 'NSObject *'

开发者 https://www.devze.com 2023-03-28 10:12 出处:网络
In registering for a callback to change with KVO, this works but makes for compiler warnings. Is this an accident (that it works), or is there some special sauce I am to apply to suppress the warnin

In registering for a callback to change with KVO, this works but makes for compiler warnings.

Is this an accident (that it works), or is there some special sauce I am to apply to suppress the warnings? Is there a global singleton '+' NSObject for each class?

[defaults addObserver:[MyClass class] forKeyPath:@"values.SomeValueThatITrack" options:options context:nil];

Then I also have in MyClass.m:

 +(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
{
    usual blah
}

Actual warning string: "Incompatible pointer types sending 'Class' to parameter of type 'NSObject *'

The KVO change is a global 开发者_如何学编程preferences related change, and as such can be dealt with by the class itself, rather than any single instance.


Casting the observer parameter eliminates the compiler warning:

[defaults addObserver:(id)[MyClass class] forKeyPath:@"values.SomeValueThatITrack" options:options context:nil];

So it appears Class can respond to selectors but isn't a subclass of NSObject. Class methods work because classes can respond to selectors.

I'm curious whether registering an instance of Class as an observer works in all situations, or if KVO requires observers to provide other functionality normally provided by NSObject.

0

精彩评论

暂无评论...
验证码 换一张
取 消