Objective-C:为什么在respondsToSelector:之前检查nil?
我见过这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.你是对的。从技术上来说,这在 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 returnnil
automatically. However, if you ignore the call torespondsToSelector:
by first checking if it'snil
, then you will skip the overhead of therespondsToSelector:
call. So it would be faster, but by how much, I'm not sure.这里有一个语法问题 - 如果你发送 -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.