有没有办法让 __PRETTY_FUNCTION__
宏只解析为 ObjC 选择器的第一部分?
-(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'
*/
}
最佳答案
然后您应该使用类似以下内容编辑该字符串:
NSString *method = [NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__];
[method substringToIndex:[method rangeOfString:@":"].location];
然后您可以用它制作一个宏以便于使用(使用 _cmd
会得到更好的结果,如 Ole 所述)
#define __LOG_PRETTY_FUNCTION__ { \
NSString *method = NSStringFromSelector(_cmd); \
NSUInteger location = [method rangeOfString:@":"].location; \
if(location == NSNotFound) NSLog(@"%@", method); \
else NSLog(@"%@", [method substringToIndex:location]); \
}
关于objective-c - 让 __PRETTY_FUNCTION__ 只打印选择器的第一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5850797/