When should I compare an optional value to nil?

前端 未结 5 1535
时光取名叫无心
时光取名叫无心 2020-11-22 10:22

Quite often, you need to write code such as the following:

if someOptional != nil {
    // do something with the unwrapped someOptional e.g.       
    someF         


        
5条回答
  •  情深已故
    2020-11-22 11:13

    We can use optional binding.

    var x:Int?
    
    if let y = x {
      // x was not nil, and its value is now stored in y
    }
    else {
      // x was nil
    }
    

提交回复
热议问题