swift 引入了命名空间的概念,所以我们要先拼接命名空间
/// 通过类名创建类
///
/// - Parameter className: 类名
/// - Returns: 类遵循的协议
func getJumpViewController(className:String) -> someOneAnswerProtocol {
//1.这是获取命名空间
guard let name = Bundle.main.object(forInfoDictionaryKey: "CFBundleExecutable") as? String else {
return DataAnswerViewController.init()
}
//2.命名空间和类名拼接
let className = name + "." + "DataAnswerViewController"
//3.转化为类
let objectClass:AnyClass? = NSClassFromString(className)
//4.swift 中通过class创建一个对象必须告诉系统class类型
guard let classType = objectClass as? UIViewController.Type else {
return DataAnswerViewController.init()
}
//4.通过Class创建对象
let handleObject = classType.init()
return handleObject as! someOneAnswerProtocol
}