Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_cmd value inside C functions

What's the value of _cmd variable when I access it from C-style function's body?

Is it defined inside Objective-C methods only?

P.S. This question may originate from my non-understanding of what _cmd is. I would greatly appreciate if someone provided me with a good explanation source.

like image 344
wh1t3cat1k Avatar asked Dec 11 '10 18:12

wh1t3cat1k


2 Answers

The major use of the _cmd function is to get the method name in which it is called.

The use of the _cmd with some other functions has been written below.

NSLog(@"<%@:%@:%d>", NSStringFromClass([self class]), NSStringFromSelector(_cmd), __LINE__);

Instead of above line you can also use PrettyFunction

NSLog(@"%s", __PRETTY_FUNCTION__); 
like image 50
Abhilash Reddy Kallepu Avatar answered Sep 26 '22 01:09

Abhilash Reddy Kallepu


It's for Objective-C methods only, so you can't access it. The first two parameters passed to all Objective-C methods are self and _cmd, then whatever other arguments the actual method takes. Since neither self nor _cmd are passed to regular C functions, you can't access them.

There's nothing particularly magic about either variable.

like image 26
Jason Coco Avatar answered Sep 22 '22 01:09

Jason Coco