• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
why isn't id<MyCellDelegate> an id?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: why isn't id<MyCellDelegate> an id?
      • From: Greg Parker <email@hidden>
    • Re: why isn't id<MyCellDelegate> an id?
      • From: Jens Alfke <email@hidden>
    • Re: why isn't id<MyCellDelegate> an id?
      • From: Sean McBride <email@hidden>
    • Re: why isn't id<MyCellDelegate> an id?
      • From: Kyle Sluder <email@hidden>
    • Re: why isn't id<MyCellDelegate> an id?
      • From: David Duncan <email@hidden>
  • Prev by Date: C++ Function Call from Obj-C
  • Next by Date: Doc Viewer in 5.0.1
  • Previous by thread: Re: C++ Function Call from Obj-C
  • Next by thread: Re: why isn't id<MyCellDelegate> an id?
  • Index(es):
    • Date
    • Thread