使用字符串获取类对象NSClassFromString

比如:

String *className = @"AboutVC";
UIViewController *viewController = [[NSClassFromString(className) alloc] init];

相对于:

UIViewController *viewController = [[AboutVC alloc] init];

的好处大致是两点:

1、前者方式,不用声明AboutVC类的导入

2、字符串是更灵活的

posted @ 2023-10-21 17:26  码出境界  阅读(35)  评论(0编辑  收藏  举报