I have a special NSLog method swizzle that prints the current method as part of the log statement.
For Objective-C methods, I have a macro that uses: NSStringFromSelector(_cmd)
For non Objective-C methods, I have a second macro that uses __PRETTY_FUNCTION__ because _cmd is not defined.
I am now trying to use the same macro for both, so I am trying to test if it is defined and thought I could do so like this:
NSString *command = (&_cmd != nil ? NSStringFromSelector(_cmd) : [NSString stringWithCString:__PRETTY_FUNCTION__ encoding:NSUTF8StringEncoding])
but I am still getting some Use of undeclared identifier '_cmd' errors when the macro is used in non Objective-C methods. How can I do a runtime test do determine if _cmd is defined or not?
You cannot test at runtime whether
_cmdis defined.The
__PRETTY_FUNCTION__macro is defined in Objective-C methods. Just use__PRETTY_FUNCTION__everywhere and don’t try to use_cmd.Example:
Here’s the output: