当我也可以正常调用该方法时,我应该使用performSelector:吗?
我想调用通过[self delegate]
获得的对象的方法。我知道它是哪个类,因此我可以导入该类并正常调用它,但我也可以使用 performSelector:
,它不需要导入我的类。我不需要将参数传递给该方法。是的,我确实读过这个。在这种情况下,哪一个更可取?
I want to call a method on an object which I get through [self delegate]
. I know which class it is so I can import the class and call it normally but I could also use performSelector:
which doesn't require importing my class. I do not need to pass a parameter to the method. And yes, I did read this. Which one is preferable in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
直接调用方法更具可读性。
performSelector:
应该在您需要更高阶消息传递时保留。严格来说,您不需要导入该类来向其发送消息,因为消息调度是动态的而不是静态的,尽管您会收到编译时警告,表明对象可能不会响应选择器。
Calling the method directly is more readable.
performSelector:
should be reserved for when you need higher order messaging.Strictly speaking, you don't need to import the class to send it a message as message dispatch is dynamic rather than static, though you will get compile time warnings that the object may not respond to the selector.
一般来说,反射操作(例如
performSelector:
)的效率低于直接操作。不过,我必须承认我对 objC 不是很熟悉。Generally speaking, reflective operations, such as
performSelector:
, are less efficient than the direct ones. I have to admit that I am not very familiar with objC, though.