How to unwrap double optionals?

后端 未结 5 656
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 13:59

How do you unwrap a string that is returned as:

(Optional(Optional \"blue\"))

var cityName = String()

if let cityAnno =         


        
5条回答
  •  难免孤独
    2020-11-27 14:56

    This is more of an important comment.

    Think of a double optional as such:

    indirect enum S {
      case value(s: S?)
      case none
    }
    

    While a normal optional is like this:

    enum S {
      case value(s: S)
      case none
    }
    

    understanding why indirect is used in the first example is unimportant to scope of the question asked. It just doesn't compile without it! In the first example I was just trying to show that its internal type is also an optional type

提交回复
热议问题