• 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: respondsToSelector - #import OR compiler warnings?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: respondsToSelector - #import OR compiler warnings?


  • Subject: Re: respondsToSelector - #import OR compiler warnings?
  • From: Bill Bumgarner <email@hidden>
  • Date: Tue, 30 Dec 2008 15:12:30 -0800

I should have also mentioned -- now that I can :) -- that there is a new pattern for declaring informal protocols that is pervasive across the iPhone's UIKit.

Specifically, the use of @optional in an @protocol to declare all methods in the protocol as optional.

For example, you could declare:

@protocol BobDelegate
@optional
- (void) fooBar: (Baz *) fred;
@end

@interface Bob:NSObject
{
	id <BobDelegate> _delegate;
}

- (void) setDelegate: (id <BobDelegate>) aDelegate;
- (id <BobDelegate>) delegate;
@end

Thus, if you wanted to use an instance of MyClass as a delegate, MyClass would have to be declared like:

@interface MyClass : NSObject <BobDelegate>
@end

MyClass doesn't have to actually implement *any* of the methods in BobDelegate (if they are *all* marked @optional, anyway).

This pattern offers a couple of advantages:

(1) It is extremely precise in intention and scoping.

(2) The _delegate will *only* respond to methods declared in BobDelegate (the compiler will warn for any others, even -description and other NSObject methods). This is exactly correct.

(3) It gives you something to easily cmd-dbl-click in Xcode to see what methods are contained in the protocol.

You can also mix-and-match @optional and @required methods. Consider NSTableViewDataSource. It has two methods that are required and several that are optional. This could be exactly declared via an @protocol.

b.bum

_______________________________________________

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: respondsToSelector - #import OR compiler warnings?
      • From: mmalc crawford <email@hidden>
    • Re: respondsToSelector - #import OR compiler warnings?
      • From: "Kyle Sluder" <email@hidden>
References: 
 >respondsToSelector - #import OR compiler warnings? (From: Steve Cronin <email@hidden>)
 >Re: respondsToSelector - #import OR compiler warnings? (From: Graham Cox <email@hidden>)
 >Re: respondsToSelector - #import OR compiler warnings? (From: Steve Cronin <email@hidden>)
 >Re: respondsToSelector - #import OR compiler warnings? (From: Steve Cronin <email@hidden>)
 >Re: respondsToSelector - #import OR compiler warnings? (From: Bill Bumgarner <email@hidden>)

  • Prev by Date: iPhone ripple effect
  • Next by Date: Re: Input Method Kit Sample Code
  • Previous by thread: Re: respondsToSelector - #import OR compiler warnings?
  • Next by thread: Re: respondsToSelector - #import OR compiler warnings?
  • Index(es):
    • Date
    • Thread