<分区>
Possible Duplicate:
How to pass all arguments of a method into NSLog?
我可以设置一个宏 printCommand 来记录方法调用的接收者和选择器,如下所示:
#define printMethodCall NSLog (@"%@ %@", self, NSStringFromSelector(_cmd));
问题——上面的内容是否可以扩展为记录随方法调用传递的所有参数,无论数量多寡,类型如何?
<分区>
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/