是否有扩展至当前选择器的预处理器宏?
这适用于 Objective-C,是否有预处理器宏或其他东西可以获取当前选择器的 SEL 值?具体来说,我正在寻找类似的东西:
-(void) someSelector
{
SEL mySelector = __CURRENT_SELECTOR__;
NSLog(@"I'm in selector %@",NSStringFromSelector(mySelector));
}
它有点像 __FILE__ 宏,但这是为了获取当前选择器。将其传递给其他人非常有用,同时不用担心在选择器名称更改时更新它。
Possible Duplicate:
Dynamically retrieving current method's name
Obj-C introspection: How can a method reference it's own selector?
This applies to Objective-C, is there a preprocessor macro or something to get the SEL value of the current selector? Specifically I'm looking for something like:
-(void) someSelector
{
SEL mySelector = __CURRENT_SELECTOR__;
NSLog(@"I'm in selector %@",NSStringFromSelector(mySelector));
}
it's kinda like the __FILE__
macro but this to obtain the current selector. Pretty useful to pass it to others while not worrying to update it if the selector name is changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个方法都有两个隐式参数,
self
,它是一个id
(接收者)和一个名为_cmd
的SEL
,这可能就是你想要的。请注意,这与预处理器或编译时之前的任何内容无关,
_cmd
不是宏,而是参数。Every method has two implicit parameters,
self
which is anid
(the receiver) and aSEL
called_cmd
, which is probably what you want.Note that this has nothing to do with preprocessor or anything before compile-time,
_cmd
is not a macro, it's an argument.sidyl所说的应该可以回答你的问题。只是想添加,如果您只需要它进行日志记录,您也可以使用常用的 C 关键字,例如
What sidyll said should answer your question. Just wanted to add if you just need it for logging you can also use the usual C keywords, e.g.
_cmd 将为您提供当前选择器(仅在 Objective-C 中可用)
_cmd will get you the current selector(only available in objective-c)