Possible to determine who called a method?

  • Hi

    Say you have several objects "A," "B," "C," which contain references
    to each other in an NSArray. Is it possible inside each object's
    doSomethingAmazing method to determine which object called it? For
    example: Say Object C receives a doSomethingAmazing invocation. It
    should do its thing and then notify it's siblings to run their own
    doSomethingAmazing methods but because each contains references to
    all the others, it could enter an infinite loop. Here's an example:

    - (void) doSomethingAmazing
    {
    /* do amazing stuff here */

    // now tell objects in refList to run their own doSomethingAmazing
    methods
    NSEnumerator *enumerator  = [refList objectEnumerator];
    id ref;

    while (ref = [enumerator nextObject])
    {
      if (ref != callingObject)
      [ref doSomethingAmazing];
    }
    }

    I know I could do this by adding another argument (say "caller") to
    the doSomethingAmazing method but would like to avoid that if possible.

    Anyone know if caller detection is possible?

    Thanks for any help

    Ken
  • No, this is not properly possible. It rather breaks encapsulation if
    it did work. I would go with adding a "caller" or similar argument to
    the method. Rather like IB does:

    - (IBAction)doSomething:(id)sender;

    On 1 Nov 2007, at 18:19, Ken Tozier wrote:

    > Hi
    >
    > Say you have several objects "A," "B," "C," which contain references
    > to each other in an NSArray. Is it possible inside each object's
    > doSomethingAmazing method to determine which object called it? For
    > example: Say Object C receives a doSomethingAmazing invocation. It
    > should do its thing and then notify it's siblings to run their own
    > doSomethingAmazing methods but because each contains references to
    > all the others, it could enter an infinite loop. Here's an example:
    >
    > - (void) doSomethingAmazing
    > {
    > /* do amazing stuff here */
    >
    > // now tell objects in refList to run their own doSomethingAmazing
    > methods
    > NSEnumerator    *enumerator        = [refList objectEnumerator];
    > id ref;
    >
    > while (ref = [enumerator nextObject])
    > {
    > if (ref != callingObject)
    > [ref doSomethingAmazing];
    > }
    > }
    >
    > I know I could do this by adding another argument (say "caller") to
    > the doSomethingAmazing method but would like to avoid that if
    > possible.
    >
    > Anyone know if caller detection is possible?
    >
    > Thanks for any help
    >
    > Ken
  • On Nov 1, 2007, at 2:19 PM, Ken Tozier wrote:

    > Hi
    >
    > Say you have several objects "A," "B," "C," which contain
    > references to each other in an NSArray. Is it possible inside each
    > object's doSomethingAmazing method to determine which object called
    > it? For example: Say Object C receives a doSomethingAmazing
    > invocation. It should do its thing and then notify it's siblings to
    > run their own doSomethingAmazing methods but because each contains
    > references to all the others, it could enter an infinite loop.
    > Here's an example:

    Hi Ken,

    I actually asked this question a few weeks ago. The general consenous
    is that it is not possible within the constructs of the language.
    However, you can add a "sender" parameter to your methods to get the
    same result. For example...

    -(BOOL) doSomethingWith:(id) anObject sender:(id)whoMadeTheCall
    {

    }

    ...

    [class doSomethingWith:anObject sender:self];

    I've been doing this now in terms of passing sender around for
    notifications, and it works great.

    HTH,

    Jaime Magiera
    Sensory Research
    http://www.sensoryresearch.net
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today