反射创建对象和方法
class TestSW: NSObject {
override required init() {
print("init333333")
}
@objc func test(){
print("000")
}
@objc func test11(from:String){
print("111")
}
}
反射方法:
//反射创建对象
let module = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
let sw = NSClassFromString(module + ".TestSW") as! TestSW.Type;
let testSWModel = sw.init() //必须含有init方法
//被反射的方法需要在方法前面添加 @objc 修饰,可用以下方法反射
let methodSelector = NSSelectorFromString("test")
// let methodSelector = #selector(TestSW.test11(from:))
// let methodSelector = NSSelectorFromString("test11From:" )
testSWModel.perform(methodSelector)