How does Optional covariance work in Swift

前端 未结 2 503
星月不相逢
星月不相逢 2021-02-13 17:14

How does covariance work for Optionals in Swift?

Say I write the following code:

var nativeOptionalView: Optional
let button =         


        
2条回答
  •  滥情空心
    2021-02-13 17:43

    While I agree that there is probably some "compiler magic" going on, this can be accomplished in your custom implementation by casting the button to a UIView, e.g.

    var myOptionalButton = MyOptional.Some(button as UIView)
    

    or

    var myOptionalButton: MyOptional = .Some(button)
    

提交回复
热议问题