iphone - Objective-C 日志方法调用

标签 iphone objective-c ios debugging macros

<分区>

Possible Duplicate:
How to pass all arguments of a method into NSLog?

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

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

问题——上面的内容是否可以扩展为记录随方法调用传递的所有参数,无论数量多寡,类型如何?

最佳答案

是的,你可以做到这一点,但这相当困难。

诀窍是认识到方法实际上只是一个函数,并且您可以从参数创建一个 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/

关于iphone - Objective-C 日志方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6554752/

相关文章:

iphone - 将 View Controller 添加到 View 层次结构的正确方法是什么?

ios - iOS 中 safari 上的视频(仅限 iPhone)在关闭后一直打开

iphone - 如何检查按钮是否仍然按下(单击)?

android - iOS idfa 和 android advertising id 是唯一的吗?

ios - UITableView 滚动时滞后

css - 如何在浏览器窗口的边界(垂直和水平)中保持居中的 div?

ios - viewDidDisappear 时布局被破坏

ios - SwRevealview 幻灯片菜单中的 Menuviewcontroller 未居中对齐

iphone - 如何在ios 5中显示后面嵌入url的字符串

ios - 为什么此绘图代码在某些运行 iOS 8.1 的 iPad 上有效,而在其他 iPad 上无效?