deda9 / Optional-Value-Setter

Easy wrapping to the optional value

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional-Value-Setter

It's easy wrapping to the optional value and it comes from Swift Proposal: SE-0024

Soultion

infix operator ??=

func ??=<T> (lhs: T?, defaultValue: T) -> T {
    
    guard let lhs = lhs else {
        return defaultValue
    }
    
    return lhs
}

How to use it

let text: String? = nil
let newText = (text ??= "Default new text") // Then the newText will eqaul "Default new text"

About

Easy wrapping to the optional value