yohunl's iOS blog

时间顺流而下,生活逆水行舟

用来记录平时的一些心得!


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.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 respondsToSelector:@selector(mainScreen)]```,screens是一个实例(对象),这里判断它是否响应一个类方法,当然不能响应

[screens.class respondsToSelector:@selector(mainScreen)]```,screens.class是类本身,当然可以响应

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

最近的文章

谈谈改变 UIAlertView 和 UIActionSheet 的颜色

iOS8及以上应该使用的方式 对于>=iOS8来说, UIAlertView,UIActionSheet都被 UIAlertController替代了, 建立一个 UIAlertView的方式 UIA…

继续阅读
更早的文章

iOS日志获取和实时浏览器显示日志

平时我们写代码的时候,为了调试方便,总是会在代码中写入很多的NSLog(也可能是其它的日志框架等,例如大名鼎鼎的CocoaLumberjack),但是我们对于NSLog到底了解多少?NSLog的…

日志, NSLog, 实时显示继续阅读