Last active
August 29, 2015 14:12
-
-
Save jkubicek/fe2dbc7ad583ffacf2b6 to your computer and use it in GitHub Desktop.
Swift nil-check compound assignment operator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator ??= {} | |
func ??=<T>(inout optional: T?, value: T) { | |
if optional == nil { | |
optional = value | |
} | |
} | |
var id: Int? = nil | |
id ??= 1234 | |
println(id!) | |
// 1234 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment