Search code examples
objective-cinstanceintrospectionnslog

Getting object name in Objective-c


suppose i have a class Foo and an instance of this class myFoo:

Foo *myFoo;

is there any method "dispalyFooObjectName" that can display the name of the object, for exmample :

NSLog(@"i was called from %s", [myFoo dispalyFooObjectName]);

and the result will be :

i was called from myFoo

Solution

  • In most programming languages objects don't have names. Just because some variable myFoo references your object, doesn't mean that your object is "called" myFoo.

    And in most C-based languages variable names are not represented in the final executables at all (except for the names of external symbols).

    So the short answer is that there's no way to get to that information.

    If you want some "name", then you should add a name field to your Foo type.