Objective-C:为什么在respondsToSelector:之前检查nil?

发布于 2024-11-17 07:14:58 字数 443 浏览 4 评论 0原文

我见过这样的代码:

if (delegate != nil && [delegate respondsToSelector:@selector(doSomething)]) ...

但是,向 nil 发送消息只会返回 nil (其计算结果为 NO),所以为什么不直接这样做:

if ([delegate respondsToSelector:@selector(doSomething)]) ...

如果delegate == nil,前者更快吗?不管怎样,我更喜欢后者,因为它的代码更少。

而且,更好。每个 Unix 专业人士都知道这一点。

I've seen code like:

if (delegate != nil && [delegate respondsToSelector:@selector(doSomething)]) ...

But, sending a message to nil just returns nil (which evaluates to NO), so why not just do:

if ([delegate respondsToSelector:@selector(doSomething)]) ...

Is the former faster if delegate == nil? Either way, I prefer the latter cause it's less code.

And, less is better than more. Every Unix pro knows that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

云胡 2024-11-24 07:14:59

objc_msgSend,Objective-C 中用于发送动态消息的函数立即检查第一个参数(消息接收者),如果它 == nil 则返回。因此,nil 消息传递的唯一开销是动态链接库函数调用,这比“二进制内”函数调用的成本稍高。总体而言,一种方法比另一种方法更有效吗?复合条件语句通常需要额外的分支,因此如果不查看编译器生成的代码,但更重要的是分析正在运行的程序,答案是无法确定的。过早的优化是一件坏事™,但我祝贺您真正考虑了效率并质疑诸如此类的“习惯用法”。

objc_msgSend, the function used to send dynamic messages in Objective-C immediately checks the first argument (the message receiver) and returns if it == nil. Therefore, the only overhead of nil messaging is a dynamically-linked library function call, which is slightly more costly than an "intra-binary" function call. Overall, is one approach more efficient than the other? Compound conditional statements usually require additional branching, so the answer is indeterminable without looking at the code the compiler generates, but more importantly profiling the running program. Premature optimization is a Bad Thing™, but I congratulate you for actually considering efficiency and questioning "idioms" such as this.

自由如风 2024-11-24 07:14:59

你是对的。从技术上来说,这在 Obj-C 中是不必要的开销,因为发送到 nil 的任何消息都会自动返回 nil。但是,如果您通过首先检查 respondsToSelector: 是否为 nil 来忽略对 respondsToSelector: 的调用,那么您将跳过 respondsToSelector: 调用的开销。所以它会更快,但快多少,我不确定。

You are correct. This is technically unnecessary overhead in Obj-C as any message sent to nil will return nil automatically. However, if you ignore the call to respondsToSelector: by first checking if it's nil, then you will skip the overhead of the respondsToSelector: call. So it would be faster, but by how much, I'm not sure.

诺曦 2024-11-24 07:14:59

这里有一个语法问题 - 如果你发送 -respondsToSelector 到一个 nil 对象,它总是返回 nil。这就是为什么你会做这样的事情。

There's a syntactical issue here- if you're sending -respondsToSelector to a nil object, it will always return nil. This is why you would do such a thing.

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