FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);
FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
FOUNDATION_EXPORT Class _Nullable NSClassFromString(NSString *aClassName);
FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
1、通过字符串创建类和对象
// 通过NSClassFromString
Class customClass = NSClassFromString(@"XZViewController");
UIViewController *obj = [[customClass alloc] init];
obj.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:obj animated:YES];
2、通过字符串(已存在的方法名称)获取一个SEL
// 参数传递
[obj performSelector:@selector(setAccount:) withObject:account];
// 参数传递Block
void (^myBlock)(void) = ^ {
// 拖拽验证成功验证成功
[self.loginView checkPhoneNumberFormat];
};
[obj performSelector:@selector(setCheckVerifyCodeSuccessBlock:) withObject:myBlock];
网友评论