如何NSLog调用函数
不是调用 NSLog 或 Dlog 的函数,而是调用该函数的函数。
我创建了一个类,
+(void) computeTime:(void (^)())block
{
NSDate * currentTime = [NSDate date];
block();
DLog ("Time Running is: %f", [[NSDate date] timeIntervalSinceDate:currentTime);
}
因此每次有一个我想要测量其时间的操作时,我都会将其放在一个块中。
并执行[工具computeTime:^{//操作}];
但是,我想知道调用该computeTime 的函数。我该怎么做?
Not the function calling the NSLog or Dlog but function that call that function.
I created a class
+(void) computeTime:(void (^)())block
{
NSDate * currentTime = [NSDate date];
block();
DLog ("Time Running is: %f", [[NSDate date] timeIntervalSinceDate:currentTime);
}
So everytime there is an operation whose time I want to measure I will put that in a block instead.
and do [Tools computeTime:^{//operation}];
However, I want to know the function that call that computeTime. How do I do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个选择:
第一个是滥用 +[NSThread callStackSymbols] 来获取调用堆栈中所有符号的数组,并取出您想要的符号。我怀疑这会相对缓慢。
第二种是使用宏。 C 预处理器提供了一个名为 __PRETTY_FUNCTION__ 的漂亮宏,其中包含格式良好的函数名称,并且它也适用于 Obj-C 方法。您可以使用类似
[ToolscomputeTimeWithName:__PRETTY_FUNCTION__ block:^{/*operation*/}]
[ToolscomputeTime:^{/*operation*/}] > 或者您可以将其全部包装在您自己的宏中,这样您就可以说TOOLS_COMPUTE_TIME(^{/*operation*/})
:注意,我使用了 varargs 风格的宏,因为C 预处理器不能很好地理解 obj-c 语法,因此块内的任何逗号都将被解释为宏的单独参数。如果我使用 TOOLS_COMPUTE_TIME(op) 定义它,那么编译器会抱怨该宏只接受 1 个参数,但给了多个参数。通过使用可变参数,编译器不关心您给它多少个参数,它会将它们全部传递给
__VA_ARGS__
标记。Two options:
The first is to abuse
+[NSThread callStackSymbols]
to get an array of all the symbols in the call stack and pull out the one you want. I suspect this is going to be relatively slow.The second is to use a macro. The C preprocessor provides a nice macro called
__PRETTY_FUNCTION__
which contains the name of the function all nicely formatted, and it works well for Obj-C methods as well. Instead of[Tools computeTime:^{/*operation*/}]
you can either use something like[Tools computeTimeWithName:__PRETTY_FUNCTION__ block:^{/*operation*/}]
or you could wrap it all up in your own macro so you can sayTOOLS_COMPUTE_TIME(^{/*operation*/})
:Note, I've used a varargs-style macro because the C Preprocessor doesn't understand obj-c syntax very well, so any commas inside your block will be interpreted as separate arguments to the macro. If I defined it using
TOOLS_COMPUTE_TIME(op)
then the compiler would complain that the macro only takes 1 argument but it was given multiple. By using varargs the compiler doesn't care how many arguments you give it, and it will pass them all on to the__VA_ARGS__
token.对于搜索OP原始问题并添加Kevin的第一个使用调用堆栈的建议的人来说,另一个可能的答案。
如果您正在寻找所谓的函数(方法),请考虑以下事项:
查找的堆栈项可能位于 stackArray 的更深处。
希望这有助于更快地发现错误。
Another possible answer for someone searching for the OP's original question and adding to Kevin's first suggestion to use the call stack.
If you're looking for what called a function (method), consider the following:
It is possible that the stack item looked for is further into stackArray.
Hope this helps find a bug quicker.