7

As far as I understand Objective C is a much more dynamic language than C# or Java. In C# you can only know the class of the caller object (using stacktrace) but not the instance reference of the caller itself.

Is Objective C more capable for this ?

Update: I looked the other post here How can I determine the "caller" of my method in Objective-C?. As far as I understand it only gives Class Name. I insist that I'm not interested only by class name but by getting a ref to the caller instance itself.

Community
  • 1
  • 1
user310291
  • 36,946
  • 82
  • 271
  • 487
  • Rather than reiterate what you want, please explain what it is you are trying to do. Getting the callee instance is probably not possible in Objective-C, but your higher-level problem may be solvable in a different way. – Nick Lockwood Jan 29 '12 at 01:02

3 Answers3

6

This has already been answered here:

How can I determine the "caller" of my method in Objective-C?

The answer's not particularly pretty though. It looks like there's no simple, reliable way to do it other than hunting through the stack, which might be OS version or platform dependent.

Why exactly did you want to do this? Perhaps there's another solution to your problem.

Community
  • 1
  • 1
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
  • I looked the other post. As far as I understand it only gives Class Name. I'm not interested only by class name but by the instance itself. – user310291 Jan 28 '12 at 16:32
  • Then it is probably not possible. As others have said, the Objective-C message sending system revolves around the objc_msgSend() function, which takes a callee and selector parameter, but not the caller. – Nick Lockwood Jan 29 '12 at 01:00
5

As far as I know you can't get even the class of the caller. All method calls in Objective-C are routed through objc_msgSend(), which takes the receiver, a selector and the parameters of the method as parameters.

The receiver will be self and the selector _cmd. The caller isn't involved in the method call. You can only go back the stack trace in order to know where the method has been called.

Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
  • stack trace does only give class name not instance ref as far as I can read http://tombarta.wordpress.com/2008/08/01/c-stack-traces-with-gcc/ – user310291 Jan 28 '12 at 16:35
3

I think this could be a start: NSLog(@"%@", [NSThread callStackSymbols]);.

Although, I think you need to see this thread first : How to find out who called a method?

Also NSThread Documentation :)

Community
  • 1
  • 1
nacho4d
  • 43,720
  • 45
  • 157
  • 240