美文网首页
Swift 通过字符串,转成类 创建对象

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

作者: LuKane | 来源:发表于2019-04-18 16:58 被阅读0次
    // 从本地 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()
    

    相关文章

      网友评论

          本文标题:Swift 通过字符串,转成类 创建对象

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