Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
if and switch as expressions
import Foundation
let randomInt = Int.random(in: 0...3)
let spelledOut: String = {
switch randomInt {
case 0:
return "Zero"
case 1:
return "One"
case 2:
return "Two"
case 3:
return "Three"
default:
return "Out of range"
}
}()
print(spelledOut)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.