Skip to content

Instantly share code, notes, and snippets.

@Francescu
Created September 29, 2014 11:27
Show Gist options
  • Save Francescu/f3321e1aa0ea1806e550 to your computer and use it in GitHub Desktop.
Save Francescu/f3321e1aa0ea1806e550 to your computer and use it in GitHub Desktop.
Python "or" equivalent in Swift - Operator
func |<T>(a:T?, b:T) -> T {
if a != nil {
return a!
}
return b
}
// You can test it in playground
var optional:String?
var defaultValue:String = "VALUE"
var myString:String = optional | defaultValue
print(myString) //prints VALUE
optional = "TEST"
myString = optional | defaultValue
print(myString) //prints TEST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment