NSObject是OC中的基类,所有类都继承于此,这里面也给我们提供了很多与“类”和“方法”相关的方法,本文将讲解几个非常实用的方法。
Person.h
#import <Foundation/Foundation.h> @interface Person : NSObject @end</span>
Student.h
#import "Person.h" // 继承Person类 @interface Student : Person - (void)test1; - (void)test2:(NSString *)string; @end</span>
MyProtocol.h
#import <Foundation/Foundation.h> @protocol MyProtocol @end</span>
【1】判断student是否是Person类的对象
// - (BOOL)isMemberOfClass:(Class)aClass; [student isMemberOfClass:[Person class]];
// - (BOOL)isKindOfClass:(Class)aClass; [student isKindOfClass:[Person class]];
// - (BOOL)conformsToProtocol:(Protocol *)aProtocol; [student conformsToProtocol:@protocol(MyProtocol)]; // 或者使用类方法 // + (BOOL)conformsToProtocol:(Protocol *)protocol; [Student conformsToProtocol:@protocol(MyProtocol)];
// - (BOOL)respondsToSelector:(SEL)aSelector; [student respondsToSelector:@selector(test1)];
// - (id)performSelector:(SEL)aSelector; [student performSelector:@selector(test1)];
// - (id)performSelector:(SEL)aSelector withObject:(id)object; [student performSelector:@selector(test2:) withObject:@"123"]; // 最多带两个参数 //- (id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2;
(在命令行没有延迟效果,因为命令行执行完后就退出main函数了 ,在IOS部分main函数一直在执行,所以可以看到延迟效果)
<span style="font-family:SimHei;">// - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; // delay单位为(秒) [student performSelector:@selector(test2:) withObject:@"123" afterDelay:2];</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013457022/article/details/47005923