美文网首页
NSClassFromString和NSSelectorFrom

NSClassFromString和NSSelectorFrom

作者: 光之盐汽水 | 来源:发表于2020-04-30 15:02 被阅读0次
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];

相关文章

网友评论

      本文标题:NSClassFromString和NSSelectorFrom

      本文链接:https://www.haomeiwen.com/subject/cckgwhtx.html