简单工厂模式与OC反射机制(2)

// - (BOOL)conformsToProtocol:(Protocol *)aProtocol;       
[student conformsToProtocol:@protocol(MyProtocol)];     
 
// 或者使用类方法       
// + (BOOL)conformsToProtocol:(Protocol *)protocol;       
[Student conformsToProtocol:@protocol(MyProtocol)];

4. 判断student的test1方法是否响应(即:是否声明并实现了test1方法)

// - (BOOL)respondsToSelector:(SEL)aSelector;       
[student respondsToSelector:@selector(test1)];

5. 间接调用student的test1方法(test1无参数)

// - (id)performSelector:(SEL)aSelector;       
[student performSelector:@selector(test1)];

6. 间接调用student的test2方法(test2有一个参数)

// - (id)performSelector:(SEL)aSelector withObject:(id)object;       
[student performSelector:@selector(test2:) withObject:@"123"];     
 
// 最多带两个参数       
//- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;

7. 延迟2s调用student的test1方法

(在命令行没有延迟效果,因为命令行执行完后就退出main函数了 ,在IOS部分main函数一直在执行,所以可以看到延迟效果)

// - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;       
// delay单位为(秒)       
[student performSelector:@selector(test2:) withObject:@"123" afterDelay:2];

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9f7a7913f92dbac6a9c720f311b9c57a.html