FROM : Fritz Anderson
DATE : Tue Nov 23 21:15:23 2004
On 19 Nov 2004, at 1:51 PM, Bruce Toback wrote:
> Is there some developer information somewhere on the best way to
> implement delegation in my own frameworks?
>
> Currently, when one of my objects with a delegate intends to call a
> delegate method, it does something like this:
>
> if ([delegate
> respondsToSelector:@selector(myObject:wantsToTellYou:)])
> [delegate myObject: self wantsToTellYou: something];
>
> There are three things that seem to be wrong with this:
>
> 1. I'm testing to see if the delegate implements a method on every
> message to the delegate. Obviously, I could cache the results of
> the test the first time through. Is that worth the effort for
> methods that aren't called in tight loops or something?
Assuming myObject:wantsToTellYou: is a method that is mandatory for
delegates of your class, you could put into the setDelegate: method
NSAssert1([proposedDelegate
respondsToSelector:@selector(myObject:wantsToTellYou:)],
@"%@ isn't a legitimate delegate", proposedDelegate);
If the delegate could be set in a NIB, do the same thing, checking that
the delegate member is non-nil first. Apple's practice, I believe, is
to cache the type-checking of delegates in this way. You really need to
detect this only once, as only a logical error in your code would
trigger the assertion.
-- F
--
Fritz Anderson
Consulting Programmer Chicago, Illinois
http://resume.manoverboard.org/
DATE : Tue Nov 23 21:15:23 2004
On 19 Nov 2004, at 1:51 PM, Bruce Toback wrote:
> Is there some developer information somewhere on the best way to
> implement delegation in my own frameworks?
>
> Currently, when one of my objects with a delegate intends to call a
> delegate method, it does something like this:
>
> if ([delegate
> respondsToSelector:@selector(myObject:wantsToTellYou:)])
> [delegate myObject: self wantsToTellYou: something];
>
> There are three things that seem to be wrong with this:
>
> 1. I'm testing to see if the delegate implements a method on every
> message to the delegate. Obviously, I could cache the results of
> the test the first time through. Is that worth the effort for
> methods that aren't called in tight loops or something?
Assuming myObject:wantsToTellYou: is a method that is mandatory for
delegates of your class, you could put into the setDelegate: method
NSAssert1([proposedDelegate
respondsToSelector:@selector(myObject:wantsToTellYou:)],
@"%@ isn't a legitimate delegate", proposedDelegate);
If the delegate could be set in a NIB, do the same thing, checking that
the delegate member is non-nil first. Apple's practice, I believe, is
to cache the type-checking of delegates in this way. You really need to
detect this only once, as only a logical error in your code would
trigger the assertion.
-- F
--
Fritz Anderson
Consulting Programmer Chicago, Illinois
http://resume.manoverboard.org/
| Related mails | Author | Date |
|---|---|---|
| Bruce Toback | Nov 19, 20:51 | |
| Joakim Danielson | Nov 19, 23:56 | |
| Bruce Toback | Nov 20, 00:08 | |
| Bruce Toback | Nov 20, 00:12 | |
| Douglas A. Welton | Nov 20, 14:28 | |
| Tobias Peciva | Nov 21, 21:35 | |
| Fritz Anderson | Nov 23, 21:15 |






Cocoa mail archive

