protocol TestProtocol {
init()
}
class Person: NSObject, TestProtocol {
required override init() {
super.init()
}
}
class Man: Person {
}
class Women: Person {
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let classes: [TestProtocol.Type] = [Person.self, Man.self, Women.self]
classes.forEach { (type) in
let obj = type.init()
print(obj)
}
}
}
我尝试在 Xcode10.2 中执行这些代码,将 Swift 版本配置为 Swift5,我希望获得 Person、Man 和 Women 的实例,但控制台结果是:
<TestSwift5.Person: 0x6000006eb3e0>
<TestSwift5.Person: 0x6000006eb3e0>
<TestSwift5.Person: 0x6000006eb3e0>
这让我很困惑,任何人都可以解释它。
期待你的回答,谢谢。