您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页iOS字符串初始化类

iOS字符串初始化类

来源:二三娱乐

从字符串 "ViewController"生成对应类的实例 Objective-C & Swift

现在有一个类ViewController

@interface ViewController 
@end

@implementation ViewController
@end

需求: 从字符串 "ViewController"生成对应类的实例

在OC中有一个简单的反射方法 NSClassFromString

ViewController *controller = [NSClassFromString([ViewController class]) new];

那么如何在swift中, 如何实现上面的功能呢

let vcClass = NSClassFromString(classString) as! UIViewController.Type
let viewController = vcClass.init()

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务