Swift4中String转为控制器的方法

参考:https://www.jianshu.com/p/df4390b43dd6

func stringToViewController(controllerName:String) -> UIViewController{
        let namespace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        let cls:AnyClass = NSClassFromString(namespace + "." + controllerName)!
        
        let vcCls = cls as! UIViewController.Type
        
        let vc = vcCls.init()
        
        return vc
    }

你可能感兴趣的:(Swift4中String转为控制器的方法)