动态检索当前方法的名称 [英] Dynamically retrieving current method's name

查看:75
本文介绍了动态检索当前方法的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够为项目中的所有类添加当前类名和方法名的跟踪消息.

I'd like to be able to put trace messages of the current class name and method name for all classes in my project.

是否有一种在运行时获取当前方法名称的方法,类似于:

Is there a way to get the current method's name at run time, similar to:

NSLog(@"classname: %@", [self className]);

self是从NSObject派生的类吗?

where self is a class derived out of NSObject?

推荐答案

NSLog(@"method name: %@", NSStringFromSelector(_cmd));

_cmd是所有Objective-C方法都接收到的隐藏参数(如self).它的值是用于调用该方法的选择器.

_cmd is a hidden argument (like self) that all Objective-C methods receive. Its value is the selector that was used to call the method.

这篇关于动态检索当前方法的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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