7

If I have a call from within a random class like this:

@implementation SomeClass

- (void) classMethodFoo
{
    int a = [SomeSingleton sharedInstance].aValue;
}

@end

Inside SomeSingleton sharedInstance, is there a way to get a reference to the object which called this method (without the called passing self as a parameter of course)?

Lebyrt
  • 1,376
  • 1
  • 9
  • 18
Egil
  • 4,265
  • 4
  • 20
  • 13
  • 1
    Almost duplicate: [How to find out who called a method?](http://stackoverflow.com/questions/1793999/how-to-find-out-who-called-a-method/1794051) – DarkDust May 25 '11 at 06:32
  • @DarkDust, no it is completely different question. This one is about particular instance of caller. Your link more about stack trace. – m8labs Jun 25 '13 at 06:59
  • @Timur: In both questions, it's about "who called me". You can answer that only via the stack, and there's no reliable way for that. – DarkDust Jun 25 '13 at 08:27

1 Answers1

7

No, information about the caller isn't passed along automatically.

This is why IBAction methods, for instance, have a sender parameter, and why delegate methods often have a parameter that refers to the delegate's object.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • 3
    I'd like to add that while it's possible to [get the call stack](http://stackoverflow.com/questions/5788850/see-objc-calls-in-call-stack) you can't get the calling object without platform dependent stack-walking (as far as I can see). – DarkDust May 25 '11 at 06:29
  • 3
    Also, it may be the case that no ‘object’ called that method. For instance, that method could be called from a function. –  May 25 '11 at 06:32
  • @Dark: Thanks for the link. I wasn't sure if it was necessary to include a disclaimer that it is actually possible if one is willing to dig into internals that one most likely doesn't want to dig into. :) – jscs May 25 '11 at 06:33