Skip navigation.
 
mlImplementing delegation
FROM : Bruce Toback
DATE : Fri Nov 19 20:51:42 2004

Hi all,

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?

2. Because delegate is declared as id in my object, I get a compiler
    warning on every delegate call. This is annoying.

3. Although it hasn't come up yet, the compiler assumes that the
    return type of the delegate method is id. That may not always
    be the case (and isn't now, but all delegate methods I've
    defined up to now return void).

To get around 2 and 3, should I just declare a protocol, and then
declare the delegate as a pointer to an object implementing that
protocol -- even though the protocol is informal? Is there any downside
to that? Or is there a widely-accepted set of idioms used to implement
delegation, and I'm just missing them?

-- Bruce

------------------------------------------------------------------------
Bruce Toback      (602) 996-8601| My candle burns at both ends;
OPT, Inc.          (800) 858-4507| It will not last the night;
11801 N. Tatum Blvd. Ste. 142    | But ah, my foes, and oh, my friends -
Phoenix AZ 85028                | It gives a lovely light.
<email_removed>                |    -- Edna St. Vincent Millay

Related mailsAuthorDate
mlImplementing delegation Bruce Toback Nov 19, 20:51
mlRe: Implementing delegation Joakim Danielson Nov 19, 23:56
mlRe: Implementing delegation Bruce Toback Nov 20, 00:08
mlRe: Implementing delegation Bruce Toback Nov 20, 00:12
mlRe: Implementing delegation Douglas A. Welton Nov 20, 14:28
mlRe: Implementing delegation Tobias Peciva Nov 21, 21:35
mlRe: Implementing delegation Fritz Anderson Nov 23, 21:15