Get __PRETTY_FUNCTION__ to only print the first part of the selector?
Is there a way to make the __PRETTY_FUNCTION__
macro only resolve to the fir开发者_运维问答st part of an ObjC selector?
-(void)arg1:(int)a1 arg2:(int)a2 arg3:(int)a3 {
printf("%s\n",__PRETTY_FUNCTION__);
/* Prints 'arg1:arg2:arg3'
I want it to print 'arg1'
*/
}
Then you should edit that string by using something like:
NSString *method = [NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__];
[method substringToIndex:[method rangeOfString:@":"].location];
You could then make a macro out of it for easy usage (using _cmd
gives better results, as noted by Ole)
#define __LOG_PRETTY_FUNCTION__ { \
NSString *method = NSStringFromSelector(_cmd); \
NSUInteger location = [method rangeOfString:@":"].location; \
if(location == NSNotFound) NSLog(@"%@", method); \
else NSLog(@"%@", [method substringToIndex:location]); \
}
No. But you could easily write your own macro that takes _cmd
(one of the two hidden arguments passed to each method), converts it to a string with NSStringFromSelector()
and parses the string to remove everything after and including the first colon it finds.
精彩评论