客观C日志方法调用 [英] objective C log method call

查看:117
本文介绍了客观C日志方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将方法的所有参数传递到NSLog?


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

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

问题 - 可以将上述内容扩展为记录通过方法调用传递的所有参数,但是很少或很多,以及任何类型的,可能是?

解决方案

是的,你可以做到这一点,但这是非常困难的。 p>

诀窍是意识到一个方法真的只是一个功能,你可以从参数中创建一个 va_list ,即使没有声明方法/函数在签名中取得 ...



伪代码将大致如下:

  va_list args; 
//在_cmd参数
va_start(args,_cmd)之后启动参数列表;
//获取这个(实例)方法的方法
方法thisMethod = class_getInstanceMethod([self class],_cmd);
//获取此方法的类型编码字符串
const char * methodType = method_getTypeEncoding(thisMethod);
//使用类型编码字符串来创建NSMethodSignature
NSMethodSignature * signature = [NSMethodSignature signatureWithObjCTypes:methodType];

//遍历所有参数,跳过0(self)和1(_cmd)
for(NSUInteger i = 2; i <[signature numberOfArguments]; ++ i){
//获取参数的类型
const char * type = [signature getArgumentTypeAtIndex:i];

//如果此参数类型与对象相同,则拉出一个对象
if(strcmp(@encode(id),type)== 0){
id nextArg = va_arg(args,id);
NSLog(@object argument:%@,nextArg);

//如果此参数类型与浮点数相同,则拉出一个浮点数
} else if(strcmp(@encode(float),type)== 0){
float nextArg = va_arg(args,float);
NSLog(@float参数:%f,nextArg);
} ...

//根据需要重新记录所有您要记录的类型
}
// cleanup
va_end(args);

幸运的是,其他人以前想要这样的事情,几乎完全相同的机制。以下是一个例子: NSLog 一个任意表达式:



http://vgable.com/blog/2010/08/ 19 /最有用的目标c-code-ive-ever-written /


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

I can setup a macro printCommand to log the receiver and selector of a method call as follows:

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

Question -- can the above be extended to log all arguments that were passed with the method call, however few or many, and whatever types, they may be?

解决方案

Yes you can do this, but it's quite difficult.

The trick is to realize that a method is really just a function, and that you can create a va_list from the arguments, even if the method/function wasn't declared to take ... in the signature.

The pseudo code would be something roughly like this:

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);

Fortunately, other people have wanted this sort of thing before and have come up with pretty much this same mechanism for doing it. Here's an example of something that will NSLog an arbitrary expression:

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

这篇关于客观C日志方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆