4

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.

Tom Andersen
  • 7,132
  • 3
  • 38
  • 55
  • Why are you doing this rather than just creating a singleton? This is not how it is usually done, so you're taking the less-tested path at the very least. – Chuck Aug 15 '11 at 21:10
  • Singletons have all sorts of code associated with them, and they need special treatment with threading, etc. So to do singletons, I would need to create a whole special simple class that is really just an holder for a simple singleton that can only have global meaning. Its simpler to just have a + method in the proper class. – Tom Andersen Aug 29 '11 at 19:44
  • I wouldn't consider taking a probably untested codepath that goes against both idiom and the framework's design to be simpler than copying and pasting 8 lines of code, but I guess it's your codebase to maintain. – Chuck Aug 29 '11 at 20:55

1 Answers1

3

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.

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
  • Class isn't a class, so it can't be a subclass of NSObject and doesn't have instances. It's just a bare type indicating a pointer to a class object. Classes inheriting from NSObject do get its methods, though. – Chuck Aug 16 '11 at 02:41
  • Though classes are objects, Class isn't a class. That makes sense, though it's a confusing arrangement. Updated what I wrote. – paulmelnikow Aug 18 '11 at 03:42
  • I am a little late to this thread but is calling observeValueForKeyPath on a Static class a good idea? It didn't work for me. This thread also says the same. http://stackoverflow.com/questions/9078595/how-does-addobserverforkeypath-work-on-a-static-class – shshnk Dec 16 '14 at 09:25
  • Yeah, it works fine. Here a static class is observing a real instance. That question is about observing a static class. – paulmelnikow Dec 16 '14 at 18:00