swift 通过字符串名转化成类 (NSClassFromString)

let preStr = Bundle.main.infoDictionary?["CFBundleExecutable"] as? String
        log("prestr: \(String(describing: preStr))")
if var _preStr = preStr {
  //如果项目名中有  — 需要做一下 替换 ,否则不需要
  _preStr =  _preStr.replacingOccurrences(of: "-", with: "_")
   let vcClass = NSClassFromString(_preStr + vcTitleArr[indexPath.row]) as! UIViewController.Type
   let vc = vcClass.init()
   self.navigationController?.pushViewController(vc, animated: true)
  }

你可能感兴趣的:(swift 通过字符串名转化成类 (NSClassFromString))