https://stackoverflow.com/questions/30837085/whats-the-purpose-of-double-question-mark-in-swift
https://stackoverflow.com/questions/30772063/operator-in-swift
http://kayley.name/2016/02/swifts-nil-coalescing-operator-aka-the-operator-or-double-question-mark-operator.html
http://laurencaponong.com/double-question-marks-in-swift/
1 |
let result = givenName ?? ""; |
It is called nil coalescing operator. If highs is not nil than it is unwrapped and the value returned. If it is nil then “” returned. It is a way to give a default value when an optional is nil.
Technically, it can be understood as:
1 |
let something = (a != nil ? a! : b) |
More Examples
1 2 3 4 5 6 7 8 |
var anOptionalWithValue: Int? = 28 let aValue = 13 var theResult: Int theResult = anOptionalWithValue ?? aValue print(theResult) //theResult will print 28 |
1 2 3 4 5 6 7 8 |
var anOptional: Int? let aValue = 5 var theResult: Int theResult = anOptional ?? aValue print(theResult) //theResult will be 5 |