This question already has an answer here:
- Swift protocol with “where Self” clause 1 answer
- Forced to cast, even if protocol requires given type 5 answers
Getting EXC_BAD_ACCESS (code=1, address=0x0) in forEach closure.
I simplified code structure to make in understandable:
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() { }
}