oc反射机制有三个用途:

1.获得Class

Class LoginViewController = NSClassFromString(@"LoginViewController");

NSString *str = NSStringFromClass([LoginViewController class]);

2.检查继承关系

// 判断对象是否为某个类的实例对象

[person isMemberOfClass:Person.class]

// 判断实例对象是都为某个类及其子类的实例

[person isKindOfClass:[person class]]

// 判断的实例对象是否实现了指定的协议

[person conformsToProtocol:@protocol(UITableViewDelegate)]

3.动态的调用方法

SEL selector = NSSelectorFromString(@"SelectorFirstParameter:SecondParameter:");

if([self respondsToSelector:selector])

{

[self performSelector:@selector(SelectorFirstParameter:SecondParameter:) withObject:@"firstParameter" withObject:@"secondParameter"];

}

- (void)SelectorFirstParameter:(NSString *)first SecondParameter:(NSString *)second{

NSLog(@"Logs %@ %@", first, second);

}

NSString *str = NSStringFromSelector(@selector(login));

05-11 22:55