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)]; //YES flag = [Test1 instancesRespondToSelector:@selector(classFun)]; //NO flag = [Test1 respondsToSelector:@selector(objectFun)]; //NO flag = [Test1 respondsToSelector:@selector(classFun)]; //YES Test1 *obj = [[Test1 alloc] init]; flag = [obj respondsToSelector:@selector(objectFun)]; //YES flag = [obj respondsToSelector:@selector(classFun)]; //NO
总结:
1. instancesRespondToSelector只能写在类名后面,respondsToSelector可以写在类名和实例名后面。
2. [类 instancesRespondToSelector]判断的是该类的实例是否包含某方法,等效于:[该类的实例 respondsToSelector]。
3. [类 respondsToSelector]用于判断是否包含某个类方法。