swift如何根据字符串,即类名创建类
申明类:required override init() 必须实现
class Model11: NSObject {
var name = "zhangsan"
required override init() {}
}
guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
print("没有获取到命名空间")
return
}
guard let model:AnyObject.Type = NSClassFromString("\(nameSpace).Model11") else {
print("没有获取到对应的类")
return
}
guard let model11 = model as? Model11.Type else{
print("没有转换成model")
return
}
let temp = model11.init()
print(temp.name)