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
1
  • This sort of reliance on variable names is a bit worrying... what are you trying to achieve? Commented Apr 4, 2011 at 10:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but is there any useful tips that i can use to log such information (define a macro?)
-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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.