开发者

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


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.


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]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜