Swift 通过字符串,转成类 创建对象

2019-04-18  本文已影响0人  LuKane
// 从本地 info.plist获取是否有这个 功能
guard let clsName = Bundle.main.infoDictionary!["CFBundleExecutable"] else {
    print("Can not find NameSpace!!")
    return;
}

let model : ViewModel = self.dataArr[indexPath.row] as! ViewModel
// 生成 当前类
let cls : AnyClass? = NSClassFromString((clsName as! String) + "." + model.controllerName!)
// 如果不是 UIViewController类型,则renturn
guard let clsType = cls as? UIViewController.Type else{
    print("Can not append")
    return;
}

let vc = clsType.init()
上一篇下一篇

猜你喜欢

热点阅读