0

Is it possible to know which function calls the current function in the objective-c?

for example, I have 100 functions give a call to function X. In the function X, are we able to define which function from 100 functions has already called?

Thanks.

chipbk10
  • 5,783
  • 12
  • 49
  • 85
  • 1
    Also - very important point by commenters in the linked question. If it is for debugging, there are good answers there. If it is for production, the only answer is - don't. Use a parameter instead. – Amadan Jun 12 '15 at 09:28
  • it's true. Using a parameter that contains the name of the function is a good way to know. Thanks. – chipbk10 Jun 12 '15 at 09:31

3 Answers3

3

I'm using a define directive :

#define CALLER_OF_METHOD NSLog(@"My Caller: [%@]", [[[[NSThread callStackSymbols] objectAtIndex:1] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]] objectAtIndex:1])

please try, let me know if it's works for you chipbk10

MGY
  • 7,245
  • 5
  • 41
  • 74
  • What's wrong with a breakpoint? – Droppy Jun 12 '15 at 09:37
  • Nothing wrong of course, maybe want to add in code. Also could be many different situations Droppy. – MGY Jun 12 '15 at 09:41
  • Breakpoint can be useful in a particular case. This method is useful for general cases. Imagine, we have 100 functions, it's useful to write all the callers out screen. – chipbk10 Jun 12 '15 at 09:43
  • You appear to have taken your answer from [here](http://stackoverflow.com/questions/1451342/objective-c-find-caller-of-method) without attribution. – Droppy Jun 12 '15 at 09:44
  • Dear Droppy, I'm trying to help, this code in my project about a year, I'm not only developer in team. Have a nice coding. – MGY Jun 12 '15 at 09:50
2

if you dont want to write any code, put a break point in the function, then on the left it will show you the calling tree and you can see what functions were called in order to get to that breakpoint

enter image description here

Fonix
  • 11,447
  • 3
  • 45
  • 74
2

Need to run application set break point in function X. When application stops at break points check functions in Debug navigator in xcode. you will know that which function called function X.See the sequence in below image.

enter image description here

sschunara
  • 2,285
  • 22
  • 31