Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key-Value Observing on a Protocol Object: Compiler Warnings on addObserver:

I have a simple protocol with a property:

@protocol StopsSource <NSObject>
@property (retain,readonly) NSArray * stops;
@end

I'm adding a key-value observer elsewhere to listen to changes to the "stops" property:

id<StopsSource> source = ...
[source addObserver:self
         forKeyPath:@"stops"
            options:NSKeyValueObservingOptionNew
            context:nil];

Code works as expected in that I get observeValueForKeyPath events when the "stops" property is changed. The real annoyance is a compiler warning on the addObserver call:

warning: '-addObserver:forKeyPath:options:context:' not found in protocol(s)

The 'addObserver' method is defined in a category to NSObject:

@interface NSObject(NSKeyValueObserverRegistration)

Is there any way to get XCode to drop this warning? It's my understanding that protocols can't adopt categories, so I'm not sure how to bring the NSKeyValueObserverRegistration methods into my protocol, short of copying the declarations into the protocol itself, which seems like a hack.

I know this is kind of a trivial problem, in that it's just a compiler warning, but I'm interested to know if there is a "right" way to address this.

like image 588
Brian Ferris Avatar asked Apr 12 '09 01:04

Brian Ferris


People also ask

What is Key-value observing?

Overview. Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views.

What is KVO Key-value observation?

KVO, which stands for Key-Value Observing, is one of the techniques for observing the program state changes available in Objective-C and Swift. The concept is simple: when we have an object with some instance variables, KVO allows other objects to establish surveillance on changes for any of those instance variables.

Can you explain KVO and how it's used on apple's platforms?

Key-Value Observing, KVO for short, is an important concept of the Cocoa API. It allows objects to be notified when the state of another object changes. That sounds very useful.


1 Answers

The real annoyance is a compiler warning on the addObserver call:

warning: '-addObserver:forKeyPath:options:context:' not found in protocol(s)

The 'addObserver' method is defined in a category to NSObject:

@interface NSObject(NSKeyValueObserverRegistration)

Is there any way to get XCode to drop this warning?

Xcode (lowercase c) is just showing you the warning; it's GCC, the compiler, that's giving you the warning in the first place.

You're confusing the class NSObject with the protocol NSObject. The NSObject class conforms to the NSObject protocol, among others, but protocols have no relation of their own to classes. Your StopsSource protocol, being a protocol, inherits from the NSObject protocol, not the NSObject class.

Your declaration only covers those two protocols, and not any specific class, so it doesn't include anything outside those protocols that the NSObject class may implement (such as KVO). That's why you get the warning.

As Jason Coco told you in his comment on your question, the solution is to change the declaration to use the NSObject class plus your protocol:

NSObject <StopsSource> *source = …;
like image 111
Peter Hosey Avatar answered Sep 26 '22 14:09

Peter Hosey