why isn't id<MyCellDelegate> an id?
why isn't id<MyCellDelegate> an id?
- Subject: why isn't id<MyCellDelegate> an id?
- From: Matt Neuburg <email@hidden>
- Date: Fri, 04 Oct 2013 09:59:53 -0700
Here's a familiar pattern, a .h file defining a protocol and a delegate that must adopt it:
@protocol MyCellDelegate;
@interface MyCell : UITableViewCell
@property (nonatomic, weak) id<MyCellDelegate> celldelegate;
@end
@protocol MyCellDelegate
@end
Here's the mystery. In the corresponding .m file:
-(BOOL)test {
return [self.celldelegate respondsToSelector:@selector(foo:)]; // ...wait for it...
}
Compile error! No known instance method for selector 'respondsToSelector:'. WTF???
Here are two ways of fixing the problem. (1) Cast to id:
-(BOOL)test {
return [(id)self.celldelegate respondsToSelector:@selector(foo:)];
}
Or, (2) inherit NSObject protocol back in the protocol definition:
@protocol MyCellDelegate<NSObject>
@end
But why is either of those necessary? Surely an id<MyCellDelegate> is, by definition, an id - which inherits from NSObject, and thus should solve the problem, just like casting to id. It is as if id<MyCellDelegate> is not an id, which makes no sense to me. Is this a Clang bug? Or am I just missing some fundamental truth?
m.
--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 7! http://shop.oreilly.com/product/0636920031017.do
iOS 7 Fundamentals! http://shop.oreilly.com/product/0636920032465.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden