ondToSelector和responsToSelector有

ondToSelector和responsToSelector有

本文介绍了Objective-C中的instanceRespondToSelector和responsToSelector有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己观察到的唯一区别是respondsToSelector的接收方可以是类或实例,而instancesRespondToSelector只能具有类接收方.还有什么使它们与众不同的呢?两者之间是否存在任何性能问题?

The only difference I observed on my own is that respondsToSelector's receiver can either be a class or an instance, while instancesRespondToSelector can only have a class receiver. What else makes them different, though? Are there any performance issues with one or the other?

推荐答案

-[NSObject respondsToSelector:]的实现如下:

- (BOOL)respondsToSelector:(SEL)aSelector {
    return class_respondsToSelector([self class], aSelector);
}

+[Class instancesRespondToSelector:]的实现方式如下:

+ (BOOL)instancesRespondToSelector:(SEL)aSelector {
    return class_respondsToSelector(self, aSelector);
}

(我在CoreFoundation上使用了 Hopper 来解决这个问题.)

(I used Hopper on CoreFoundation to figure this out.)

因此,基本上没有区别.但是,您可以在自己的类中覆盖respondsToSelector:以根据实例返回YES或NO(NSProxy这样做).您不能使用instancesRespondToSelector:来做到这一点.

So, there's basically no difference. However, you can override respondsToSelector: in your own class to return YES or NO on a per-instance basis (NSProxy does this). You can't do that with instancesRespondToSelector:.

这篇关于Objective-C中的instanceRespondToSelector和responsToSelector有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:32