How do you unwrap Swift optionals?

前端 未结 5 1187
难免孤独
难免孤独 2020-11-27 04:14

How do you properly unwrap both normal and implicit optionals?

There seems to be confusion in this topic and I would just like to have a reference for all of the way

5条回答
  •  萌比男神i
    2020-11-27 04:58

    Since Beta 5 we have also the new coalescing operator (??):

    var a : Int?
    let b : Int = a ?? 0
    

    If the optional is != nil it is unwrapped else the value on the right of the operator is used

提交回复
热议问题