NSObject常用方法
- - (BOOL)isKindOfClass:(Class)aClass //判断是否为aClass或者aClass的子类的实例,aClass可以通过[类名 class]获取
- - (BOOL)isMemberOfClass:(Class)aClass //判断是否为aClass的实例(不包括aClass的子类)
- - (BOOL)conformsToProtocol:(Protocol)aProtocol //判断对象是否实现了aProtocol协议
- + (BOOL)instancesRespondToSelector:(SEL)aSelector //判断这个类的对象是否拥有参数提供的方法aSelector
- - (BOOL)respondsToSelector:(SEL)aSelector //判断对象是否拥有参数提供的方法aSelector
- - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay //延迟调用参数提供的方法,方法所需参数用withObject传入
反射
- Class的反射
通过类名的字符串形式实例化对象
Class class = NSClassFromString(@"Student");
Student *stu = [[class alloc] init];
将类名变成字符串
Class class = [Student class];
NSString *className = NSStringFromClass(class);
- SEL的反射
通过方法的字符串形式实例化方法
SEL selector = NSSelectorFromString(@"setName:");
[stu performSelector:selector withObject:@"Mike"];
将方法变成字符串
NSStringFromSelector(@selector(setName:));
NSObject常用方法和反射
时间: 2024-10-24 07:20:13