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://www.cnblogs.com/claireyuancy/p/7082640.html