respondsToSelector和 instancesRespondToSelector的理解

respondsToSelector 和 instancesRespondToSelector是两个常用的方法,我们经常使用 someObject respondsToSelector,但是对于它和instancesRespondToSelector之间有什么区别?

首先先看两个方法的声明

@interface NSObject <NSObject>
+ (BOOL)instancesRespondToSelector:(SEL)aSelector;
@end

@protocol NSObject
- (BOOL)respondsToSelector:(SEL)aSelector;
@end

我们可以以UIScreen来进行说明

@interface UIScreen : NSObject <UITraitEnvironment>
+ (UIScreen *)mainScreen;
@property(nullable,nonatomic,strong) UIScreenMode *currentMode
@end

测试代码

    UIScreen *screens = [UIScreen mainScreen];
    if ([screens.class instancesRespondToSelector:@selector(currentMode)]){
        NSLog(@"[screens.class instancesRespondToSelector:@selector(currentMode)]能响应");
    }

    if ([screens respondsToSelector:@selector(currentMode)]){
        NSLog(@"[screens respondsToSelector:@selector(currentMode)]能响应");
    }

    if ([screens.class respondsToSelector:@selector(currentMode)]){
        NSLog(@"[screens.class respondsToSelector:@selector(currentMode)]能响应");
    }


    if ([UIScreen instancesRespondToSelector:@selector(currentMode)]){
        NSLog(@"[UIScreen instancesRespondToSelector:@selector(currentMode)]能响应");
    }

    if ([UIScreen respondsToSelector:@selector(currentMode)]){
        NSLog(@"[UIScreen respondsToSelector:@selector(currentMode)]能响应");
    }

输出

2016-06-30 16:17:24.501 TestNSlog[84989:2019395] [screens.class instancesRespondToSelector:@selector(currentMode)]能响应
2016-06-30 16:17:24.501 TestNSlog[84989:2019395] [screens respondsToSelector:@selector(currentMode)]能响应
2016-06-30 16:17:24.502 TestNSlog[84989:2019395] [screens.class respondsToSelector:@selector(currentMode)]不能响应
2016-06-30 16:17:26.181 TestNSlog[84989:2019395] [UIScreen instancesRespondToSelector:@selector(currentMode)]能响应
2016-06-30 16:17:26.181 TestNSlog[84989:2019395] [UIScreen respondsToSelector:@selector(currentMode)]不能响应

说明: instancesRespondToSelector是一个类方法,用来判断该类的的实例(即对象)是否响应某个方法
respondsToSelector 是一个实例(对象)方法,用来判断该实例(对象)是否响应某个方法

从定义看,UIScreenMode *currentMode是一个实例属性
所以[screens.class instancesRespondToSelector:@selector(currentMode)] 是能够响应的!!
[screens respondsToSelector:@selector(currentMode)] 能响应

[screens.class respondsToSelector:@selector(currentMode)],由于screens.class得到的是 类本身,所以当然不能响应实例方法了
同理[UIScreen respondsToSelector:@selector(currentMode)]不能响应!!!

测试代码

    if ([screens.class instancesRespondToSelector:@selector(mainScreen)]){
        NSLog(@"[screens.class instancesRespondToSelector:@selector(mainScreen)]能响应");
    }
    else{
        NSLog(@"[screens.class instancesRespondToSelector:@selector(mainScreen)]不能响应");
    }

    if ([screens respondsToSelector:@selector(mainScreen)]){
        NSLog(@"[screens respondsToSelector:@selector(mainScreen)]能响应");
    }
    else{
        NSLog(@"[screens respondsToSelector:@selector(mainScreen)]不能响应");
    }

    if ([screens.class respondsToSelector:@selector(mainScreen)]){
        NSLog(@"[screens.class respondsToSelector:@selector(mainScreen)]能响应");
    }
    else{
        NSLog(@"[screens.class respondsToSelector:@selector(mainScreen)]不能响应");
    }


    if ([UIScreen instancesRespondToSelector:@selector(mainScreen)]){
        NSLog(@"[UIScreen instancesRespondToSelector:@selector(mainScreen)]能响应");
    }
    else{
        NSLog(@"[UIScreen instancesRespondToSelector:@selector(mainScreen)]不能响应");
    }

    if ([UIScreen respondsToSelector:@selector(mainScreen)]){
        NSLog(@"[UIScreen respondsToSelector:@selector(mainScreen)]能响应");
    }
    else{
        NSLog(@"[UIScreen respondsToSelector:@selector(mainScreen)]不能响应");
    }

输出

2016-06-30 16:27:44.244 TestNSlog[85085:2029436] [screens.class instancesRespondToSelector:@selector(mainScreen)]不能响应
2016-06-30 16:27:45.876 TestNSlog[85085:2029436] [screens respondsToSelector:@selector(mainScreen)]不能响应
2016-06-30 16:27:47.364 TestNSlog[85085:2029436] [screens.class respondsToSelector:@selector(mainScreen)]能响应
2016-06-30 16:27:51.156 TestNSlog[85085:2029436] [UIScreen instancesRespondToSelector:@selector(mainScreen)]不能响应
2016-06-30 16:27:53.372 TestNSlog[85085:2029436] [UIScreen respondsToSelector:@selector(mainScreen)]能响应

[screens.class instancesRespondToSelector:@selector(mainScreen)],由于mainScreen是类方法,这里判断实例是否响应,当然是不能响应!!
[screens respondsToSelector:@selector(mainScreen)],screens是一个实例(对象),这里判断它是否响应一个类方法,当然不能响应
[screens.class respondsToSelector:@selector(mainScreen)],screens.class是类本身,当然可以响应

我们从中应该知道, 类 其实 也是一个兑现哥只是,是一个特殊对象!

comments powered by Disqus