How do you unwrap Swift optionals?

前端 未结 5 1182
难免孤独
难免孤独 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条回答
  •  一生所求
    2020-11-27 05:02

    You can also create extensions for particular type and unwrap safely with default value. I did the following for the same :

    extension Optional where Wrapped == String {
        func unwrapSafely() -> String {
            if let value = self {
                return value
            }
            return ""
        }
    }
    

提交回复
热议问题