views:

257

answers:

3

Objective-C uses a sophisticated message-passing system when one object calls a method on another object. I want to know if it is possible, within the called method, to determine what the calling object was?

For example:

@implementation callingClass
- (void)performTest
{
    calledObject = [[[calledClass alloc] init] autorelease];
    id result = [calledObject calledMethod];

    assert(result == this);
}
@end

@implementation calledClass
- (id)calledMethod
{
    id objectThatCalledThisMethod = ... // <-- what goes here?

    return objectThatCalledThisMethod;
}
@end

What could I write in the commented line in order to make the assertion pass when I execute performTest?

+6  A: 

Not with the runtime. All message sends ultimately work out to a function call along the lines of objc_msgSend(id receiver, SEL selector, /*method arguments*/...). As you can see, no information is passed about the object sending the message. It's probably possible to determine the calling object by walking the stack, but that way lies madness. The only practical way to tell who called the method is to give it a sender argument like all IBAction methods have.

Chuck
+2  A: 

No, you cannot determine what object called you. Well, technically, it might be possible to poke around the stack back trace, but certainly its not practical for real code.

If you look at most of the delegate methods, you can see that the standard delegate call formats look like this:

- (NSSize) windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
- (BOOL) windowShouldClose:(id)window;
- (void) windowWillMove:(NSNotification *)notification;

Note how the window (caller) is passed as the first argument, and how "window" is the first part of the method name. In the last case, the window caller is implicit in the NSNotification (notification.object is the window).

Peter N Lewis
+1  A: 

You could try and derive your own class from NSInvocation that carries the caller information. Or wrap a class around NSInvocation reimplementing some of the calls in there.

Harald Scheirich
NSInvocation also doesn't have anything about the caller — just target, selector, and arguments.
Quinn Taylor
That is why i suggested deriving a new class or wrapping it around, this might at least give you a consistent way to pass the sender into the calling function or possibly construct a data structure where the information can be pulled from at the receivers end. It does depend on what he is trying to accomplish
Harald Scheirich