3

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
tarek
  • 33
  • 1
  • 3

2 Answers2

6

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Thanks, but is there any useful tips that i can use to log such information (define a macro?) – tarek Apr 04 '11 at 09:45
-1

you can try this. override -(NSstring*)description method like this

- (NSString*)description {
     return [NSString stringWithFormat:@"I'm called from foo"];//You can also print object's properties description here.
  }

and use like this

 NSLog(@"my Foo object %@",[myFoo description]);
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256