OC中respondsToSelector方法与instance

2018-07-16  本文已影响203人  langkee

前言

主要记录一下respondsToSelector方法与instancesRespondToSelector方法的区别,对于很多人来说,平时使用得也比较多,但是可能会对这两个方法的使用弄混淆,希望写在这里对一些同学可以起到一些帮助。

结论

respondsToSelector方法

- (BOOL)respondsToSelector:(SEL)aSelector;

判断class是否实现了相应的类方法

判断实例对象是否实现了相应的实例方法

instancesRespondToSelector方法

+ (BOOL)instancesRespondToSelector:(SEL)aSelector;

判断该实例对象是否实现了相应的实例方法

实践

创建一项新的工程

新建一个类Person, 继承于NSObject

在Person.h文件中
// Person.h文件
@interface Person : NSObject
+ (void)classMethod:(NSString *)invoker;
- (void)instanceMethod:(NSString *)methodName;
@end
在Person.m文件中
//Person.m文件
@implementation Person

+ (void)classMethod:(NSString *)invoker {
    NSLog(@"method: classMethod has implemented!");
}

- (void)instanceMethod:(NSString *)methodName {
    NSLog(@"method: instanceMethod has implemented!");
}

@end
在ViewController.m文件中, 先导入Person.h文件
- (void)viewDidLoad {
    [super viewDidLoad];
    
    Person *personInstance = [Person new];
    
    if ([personInstance respondsToSelector:@selector(classMethod:)]) {
        NSLog(@" - yes - [personInstance respondsToSelector:@selector(classMethod:)]");
    } else {
        // 被执行
        NSLog(@" - no - [personInstance respondsToSelector:@selector(classMethod:)]");
    }
    
    if ([personInstance respondsToSelector:@selector(instanceMethod:)]) {
        // 被执行
        NSLog(@" - yes - [personInstance respondsToSelector:@selector(instanceMethod:)]");
    } else {
        NSLog(@" - no - [personInstance respondsToSelector:@selector(instanceMethod:)]");
    }
    
    if ([Person respondsToSelector:@selector(classMethod:)]) {
        // 被执行
        NSLog(@" - yes - [PersonClass respondsToSelector:@selector(classMethod:)]");
    } else {
        NSLog(@" - no - [PersonClass respondsToSelector:@selector(classMethod:)]");
    }
    
    if ([Person respondsToSelector:@selector(instanceMethod:)]) {
        NSLog(@" - yes - [PersonClass respondsToSelector:@selector(instanceMethod:)]");
    } else {
        // 被执行
        NSLog(@" - no - [PersonClass respondsToSelector:@selector(instanceMethod:)]");
    }
    
    if ([Person instancesRespondToSelector:@selector(classMethod:)]) {
        NSLog(@" - yes - [PersonClass instancesRespondToSelector:@selector(classMethod:)]");
    } else {
        // 被执行
        NSLog(@" - no - [PersonClass instancesRespondToSelector:@selector(classMethod:)]");
    }
    
    if ([Person instancesRespondToSelector:@selector(instanceMethod:)]) {
        // 被执行
        NSLog(@" - yes - [PersonClass instancesRespondToSelector:@selector(instanceMethod:)]");
    } else {
        NSLog(@" - no - [PersonClass instancesRespondToSelector:@selector(instanceMethod:)]");
    }
    
}

执行结果

- no - [personInstance respondsToSelector:@selector(classMethod:)]          ①
- yes - [personInstance respondsToSelector:@selector(instanceMethod:)]      ②
- yes - [PersonClass respondsToSelector:@selector(classMethod:)]            ③
- no - [PersonClass respondsToSelector:@selector(instanceMethod:)]          ④
- no - [PersonClass instancesRespondToSelector:@selector(classMethod:)]     ⑤
- yes - [PersonClass instancesRespondToSelector:@selector(instanceMethod:)] ⑥

结果分析

写到最后

遇到这种类似的API,如果有疑问,尽量用代码去验证,结果一目了然。

上一篇下一篇

猜你喜欢

热点阅读