打印出变量名称objective-C

时间:2010-05-12 12:01:14

标签: iphone objective-c debugging

继续上一个问题:Log method name in Obj-C。我只是想知道是否有办法打印变量名称。例如:

NSString *name = "vodkhang";
NCLog(@"%@", name);

我希望输出应该是:

name: vodkhang

总结一下上一篇文章,目前,我可以在打电话时打印出班级名称,方法名称和行号

NCLog(@"Hello World");
<ApplicationDelegate:applicationDidFinishLaunching:10>Hello world

#define NCLog(s, ...) NSLog(@"<%@:%d> %@", __FUNCTION__, __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__])

1 个答案:

答案 0 :(得分:35)

#define logIntVariable(x) NSLog( @"Value of %s = %d",#x, x)


- (void) myRoutine {
   int intValue = 5;

   logIntVariable(intValue);
}