目标C日志方法调用

时间:2011-07-02 02:27:13

标签: iphone objective-c ios debugging macros

  

可能重复:
  How to pass all arguments of a method into NSLog?

我可以设置一个宏printCommand来记录方法调用的接收者和选择器,如下所示:

#define printMethodCall NSLog (@"%@ %@", self, NSStringFromSelector(_cmd));

问题 - 可以将上述内容扩展为记录通过方法调用传递的所有参数,无论是少数还是多个,以及它们可能是什么类型?

1 个答案:

答案 0 :(得分:1)

是的,你可以做到这一点,但这很难。

诀窍是要意识到方法实际上只是一个函数,并且您可以从参数创建va_list,即使未声明方法/函数采用...签名。

伪代码大致是这样的:

va_list args;
// start your argument list after the "_cmd" argument
va_start(args, _cmd); 
// get the Method for this (instance) method
Method thisMethod = class_getInstanceMethod([self class], _cmd);
// get the type encoding string for this method
const char *methodType = method_getTypeEncoding(thisMethod);
// use the type encoding string to make an NSMethodSignature
NSMethodSignature *signature = [NSMethodSignature signatureWithObjCTypes:methodType];

// iterate through all the arguments, skipping 0 (self) and 1 (_cmd)
for (NSUInteger i = 2; i < [signature numberOfArguments]; ++i) {
  // get the type of the argument
  const char *type = [signature getArgumentTypeAtIndex:i];

  // if this argument type is the same as an object, pull out an object
  if (strcmp(@encode(id), type) == 0) {
    id nextArg = va_arg(args, id);
    NSLog(@"object argument: %@", nextArg);

  // if this argument type is the same as a float, pull out a float
  } else if (strcmp(@encode(float), type) == 0) {
    float nextArg = va_arg(args, float);
    NSLog(@"float argument: %f", nextArg);
  } ...

  // repeat as necessary for all the types you care to log
}
// cleanup
va_end(args);

幸运的是,其他人以前想要这样的东西,并且已经提出了相同的机制来做这件事。这是NSLog任意表达式的一个例子:

http://vgable.com/blog/2010/08/19/the-most-useful-objective-c-code-ive-ever-written/