Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
- Subject: Re: Custom Delegate - Using _delegate works but not delegate or [self delegate]
- From: Quincey Morris <email@hidden>
- Date: Tue, 26 Mar 2013 16:07:00 -0700
On Mar 26, 2013, at 14:14 , Conrad Shultz <email@hidden> wrote:
> Step #1 in my hypothetical was that in the future a *required* delegate method be added, in which case there would be no -respondsToSelector: check.
1. Your earlier suggestion of having *two* delegate objects would certainly be one way of ensuring this couldn't be a problem.
2. If there is to be only one delegate object, the correct way (in terms of ensuring compile-time compliance) would be to declare the subclass "delegate" property override like this:
@property (nonatomic) id<TKOutlineViewDelegate> delegate; // making it readwrite now
That is, any code setting a delegate would be required to provide one of the correct sort. If desirable (and perhaps it would be under the circumstances), this could be enforced at run-time by providing overrides, rather than using @dynamic:
- (id<TKOutlineViewDelegate>) delegate {
id<TKOutlineViewDelegate> delegate = [super delegate];
if ([delegate conformsToProtocol: @protocol (TKOutlineViewDelegate)])
return delegate;
else
return nil;
}
- (void) setDelegate: (id<TKOutlineViewDelegate>) delegate {
NSAssert ([delegate conformsToProtocol: @protocol (TKOutlineViewDelegate)], @"Boo boo");
[super setDelegate: delegate];
}
3. With one delegate object, if there's a legal scenario where some code might think that the outline view was merely a NSOutlineView and therefore set a delegate that was merely NSOutlineViewDelegate rather than TKOutlineViewDelegate, then conformity to TKOutlineViewDelegate is effectively optional. It would be incumbent, then, on callers of the TKOutlineViewDelegate delegate methods to either check for conformity or to check respondsToSelector, even for @required methods.
The correct approach requires knowing the answer to this question: Does TKOutlineView *require* TKOutlineViewDelegate conformity, or is it merely optional?
_______________________________________________
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