Due to Swift cool behaviors I was looking for an or equivalent in Swift. 
Something like this:
variable = value or default
I coded mine:
func |<T>(a:T?, b:T) -> T {
    if let a = a {
        return a
    }
    return b
}
But I was wondering if any default implementation of this already exists in Swift?
Edit: Thanks to answers I found the reference in the Swift book:
Nil Coalescing Operator
The nil coalescing operator (
a ?? b) unwraps an optional a if it contains a value, or returns a default valuebifais nil. The expressionais always of an optional type. The expressionbmust match the type that is stored insidea.The nil coalescing operator is shorthand for the code below:
a != nil ? a! : bThe code above uses the ternary conditional operator and forced unwrapping (
a!) to access the value wrapped insideawhenais not nil, and to returnbotherwise. The nil coalescing operator provides a more elegant way to encapsulate this conditional checking and unwrapping in a concise and readable form. (source)
Use ??:
var optional:String?
var defaultValue:String = "VALUE"
var myString:String = optional ?? defaultValue
print(myString)
                        You could use the nil coalescing operator
let variable = optional ?? default
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With