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.
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.
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.
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.
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 = …;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With