Swift - upcasting array of protocol to array of super protocol causes error

前端 未结 2 2024
有刺的猬
有刺的猬 2021-01-20 14:54

In Swift, I notice that I can upcast an object that conforms to a protocol called, let\'s say SubProtocol to another protocol called SuperProtocol

2条回答
  •  醉酒成梦
    2021-01-20 15:34

    Try this code - just checked, works fine

    let array2: [SuperProtocol] = array1.map { $0 as SuperProtocol }
    

提交回复
热议问题