I have a function – myFunc() in class A. There are multiple other classes calling this function.
How will I be able to find out which class is calling myFunc() at a particular instance?
Would someone be able to help me with this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Conceptually, this information is available in the stack, though it isn’t easy to get to. Most solutions would involve creating an exception in order to capture the stack trace, and then reading the trace. I haven’t tried this, but it might work:
Pertinent documentation: NSThread Class Reference
Note that if you want your function to behave differently depending on who is calling, DO NOT DO THIS. It’s fragile (there are no guarantees about whether the format of what
callStackSymbolsreturns will change).It’s better to simply pass a parameter into your function. If you’re dealing with a C-function callback API, there is typically a
void *“context” or “info” parameter that you can use to pass in an arbitrary pointer. This could be a pointer to your object.