Created
September 29, 2014 11:27
-
-
Save Francescu/f3321e1aa0ea1806e550 to your computer and use it in GitHub Desktop.
Python "or" equivalent in Swift - 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
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