是否有扩展至当前选择器的预处理器宏?

发布于 2024-12-28 00:10:18 字数 637 浏览 3 评论 0原文

可能的重复:
动态检索当前方法的名称
Obj-C 内省:方法如何引用它有自己的选择器?

这适用于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2025-01-04 00:10:18

每个方法都有两个隐式参数,self,它是一个 id(接收者)和一个名为 _cmdSEL,这可能就是你想要的。

请注意,这与预处理器或编译时之前的任何内容无关,_cmd 不是宏,而是参数。

-(void) someSelector
{
    NSLog(@"I'm in selector %@",NSStringFromSelector(_cmd));
}

Every method has two implicit parameters, self which is an id (the receiver) and a SEL 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.

-(void) someSelector
{
    NSLog(@"I'm in selector %@",NSStringFromSelector(_cmd));
}
望她远 2025-01-04 00:10:18

sidyl所说的应该可以回答你的问题。只是想添加,如果您只需要它进行日志记录,您也可以使用常用的 C 关键字,例如

NSLog( @"%s" , __PRETTY_FUNCTION__ );

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.

NSLog( @"%s" , __PRETTY_FUNCTION__ );
┊风居住的梦幻卍 2025-01-04 00:10:18
NSLog( @"%s" , _cmd );

_cmd 将为您提供当前选择器(仅在 Objective-C 中可用)

NSLog( @"%s" , _cmd );

_cmd will get you the current selector(only available in objective-c)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文