Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective-C中的打印电流法_Objective C_Reflection_Methods - Fatal编程技术网

Objective-C中的打印电流法

Objective-C中的打印电流法,objective-c,reflection,methods,Objective C,Reflection,Methods,如何在Objective-C中打印我使用的方法的名称 在Java/C中,我可以使用反射来实现这一点。与Objective-C类似吗?仅打印方法名称,请使用 NSLog(@"%@", NSStringFromSelector(_cmd)); 打印方法参数是一项复杂的任务。 Objective中的每条消息都转换为objmsgssend(id self、SEL、arg0…) 我们需要遍历堆栈,并将它们打印出来,正如我们在_cmd+sizeof(SEL)将为我们提供arg0的地址(但arg0的大小

如何在Objective-C中打印我使用的方法的名称


在Java/C中,我可以使用反射来实现这一点。与Objective-C类似吗?

仅打印方法名称,请使用

NSLog(@"%@", NSStringFromSelector(_cmd));  
打印方法参数是一项复杂的任务。 Objective中的每条消息都转换为
objmsgssend(id self、SEL、arg0…)

我们需要遍历堆栈,并将它们打印出来,正如我们在_cmd+sizeof(SEL)将为我们提供arg0的地址(但arg0的大小和类型未知)之后看到的那样

Method=class\u getInstanceMethod([self class],\u cmd);
unsigned nargs=method\u getNumberOfArguments(method);
void*start=\u cmd;
for(无符号i=0;i
基本实现应该是这样的


因此,如果参数是对象,那就太好了(因为我们知道参数的大小是32/64位)。否则,我们需要进行类型编码,并按参数的大小移动光标。看看,这正是你想要的,但它只能在模拟器下工作。

太棒了,谢谢。打印方法参数也有类似的方法吗?@GauravSharma我会尝试这样做,并告诉你答案。
Method method = class_getInstanceMethod([self class], _cmd);
unsigned nargs = method_getNumberOfArguments(method);
void *start = _cmd;
for(unsigned i = 0; i< nargs ; i++) {
  char *argtype = method_copyArgumentType(method, i);
  //find arg size from argtype
  // walk stack given arg zie
  free(argtype);
}