首页 > 代码库 > instancesRespondToSelector与respondsToSelector的区别

instancesRespondToSelector与respondsToSelector的区别

Test1.h

@interface Test1 : NSObject- (void)objectFun;+ (void)classFun;@end

 

Test1.m

@implementation Test1- (void)objectFun{    NSLog(@"object function");}+ (void)classFun{    NSLog(@"class function");}@end

 

测试类:

BOOL flag;flag = [Test1 instancesRespondToSelector:@selector(objectFun)]; //YESflag = [Test1 instancesRespondToSelector:@selector(classFun)]; //NOflag = [Test1 respondsToSelector:@selector(objectFun)]; //NOflag = [Test1 respondsToSelector:@selector(classFun)]; //YESTest1 *obj = [[Test1 alloc] init];flag = [obj respondsToSelector:@selector(objectFun)]; //YESflag = [obj respondsToSelector:@selector(classFun)]; //NO

 

总结:

1. instancesRespondToSelector只能写在类名后面,respondsToSelector可以写在类名和实例名后面。

2. [类 instancesRespondToSelector]判断的是该类的实例是否包含某方法,等效于:[该类的实例 respondsToSelector]。

3. [类 respondsToSelector]用于判断是否包含某个类方法。

instancesRespondToSelector与respondsToSelector的区别