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.
精彩评论