Last active
August 29, 2015 14:23
-
-
Save ivanbruel/9bcd085fd5e529d2df64 to your computer and use it in GitHub Desktop.
Easy Swift Unwrapping
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 ?-> { associativity left precedence 140 } | |
func ?-><T>(lhs: T?, rhs: ((T)->(Void))) { | |
if let value = lhs { | |
rhs(value) | |
} | |
} | |
// Usage | |
var someOptionalVariable : Int? | |
someOptionalVariable ?-> { println("This code will not be executed because it is nil. \($0)") } | |
someOptionalVariable = 2015 | |
someOptionalVariable ?-> { println("This code will be executed because it has a value of \($0)") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment