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 protected], 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 protected])

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to