• 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
Re: iPhone: @protocol and @optional
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: iPhone: @protocol and @optional


  • Subject: Re: iPhone: @protocol and @optional
  • From: Dave DeLong <email@hidden>
  • Date: Tue, 05 Jan 2010 11:58:15 -0700

@optional is a compile-time directive.  It means that objects that conform to your protocol don't *have* to implement the method when the code is compiled.  However, it has nothing to do with runtime-verification.  That is up to you (so yes, you should be using respondsToSelector:).

If you didn't have the @optional directive, the call to respondsToSelector: would be unnecessary.

Dave

On Jan 5, 2010, at 11:53 AM, John Michael Zorko wrote:

>
> Hello, all ...
>
> After [re-]reading the Apple documentation, i'm still not clear as to what @optional really does in a @protocol. I thought that declaring certain messages as @optional would make the app not crash if a certain message didn't have an implementation in a class that adopts the protocol, however i'm finding that this isn't the case:
>
> 2010-01-05 10:40:58.698 VoxityNowPlaying[14413:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[VoxityNowPlayingViewController playerURLFinished:]: unrecognized selector sent to instance 0x39138f0'
>
> ... yet my protocol is defined thusly:
>
> @protocol VoxityPlayerDelegate <NSObject>
>
> @optional
>
> - (void)playerURLFinished:(NSURL *)url;
>
> .
> .
> .
>
> @end
>
> Do I need to wrap code that sends messages to delegates with -respondsToSelector? If so, what is the point of @optional in the protocol?
>
> Regards,
>
> John

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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: iPhone: @protocol and @optional
      • From: Christian Ziegler <email@hidden>
References: 
 >iPhone: @protocol and @optional (From: John Michael Zorko <email@hidden>)

  • Prev by Date: iPhone: @protocol and @optional
  • Next by Date: Re: How to enforce a single NSDocument ... or should I do something else?
  • Previous by thread: iPhone: @protocol and @optional
  • Next by thread: Re: iPhone: @protocol and @optional
  • Index(es):
    • Date
    • Thread