Skip to content

Instantly share code, notes, and snippets.

@djtech42
Last active June 26, 2017 20:02
Show Gist options
  • Save djtech42/9e41b8254d86b8df586653d1bf1fe30b to your computer and use it in GitHub Desktop.
Save djtech42/9e41b8254d86b8df586653d1bf1fe30b to your computer and use it in GitHub Desktop.
Ternary Function for Swift for more readability
var value = 1005
print(ternary(if: value > 1000, then: "big", else: "small")) // OUTPUT: big
value = 5
print(ternary(if: value > 1000, then: "big", else: "small")) // OUTPUT: small
func ternary<T>(if condition: Bool, then value: T, else otherValue: T) -> T {
return condition ? value : otherValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment