Skip to content

Instantly share code, notes, and snippets.

@ivanbruel
Last active August 29, 2015 14:23
Show Gist options
  • Save ivanbruel/9bcd085fd5e529d2df64 to your computer and use it in GitHub Desktop.
Save ivanbruel/9bcd085fd5e529d2df64 to your computer and use it in GitHub Desktop.
Easy Swift Unwrapping
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