在forEach封闭中获取EXC_BAD_ACCESS(代码= 1,地址= 0x0)。
我简化了代码结构,使其易于理解:
func reproduceCrash() {
let instanceArray: [Class1] = [Class1(), Class2()]
let filteredInstanceArray = instanceArray.compactMap { $0 as? Protocol2 }
filteredInstanceArray.forEach { (instance) in
instance.method()
}
}
protocol Protocol1 { }
class Class1: Protocol1 { }
protocol Protocol2 where Self: Class1 {
func method()
}
class Class2: Class1, Protocol2 {
func method() { }
}