Re: respondsToSelector - #import OR compiler warnings?
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