-
-
Save sturdysturge/cdef56d8484b931f016fbb2415a715ec to your computer and use it in GitHub Desktop.
This file contains 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
//An array of strings that specify their indices | |
let strings: Array = ["zero", "one"] | |
//Set to default "two" | |
let one = strings[2, default: "two"] | |
//Set to default nil | |
let twoNil = strings[2, default: nil] | |
//Optionally binding the value from a valid index | |
if let one = strings[1, default: nil] { | |
print(one) //prints "one" | |
} | |
//Optionally binding the value from an invalid index | |
if let twoNilUnwrapped = strings[2, default: nil] { | |
//Never runs because nil is returned | |
print(twoNilUnwrapped) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment