Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created January 13, 2021 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/cdef56d8484b931f016fbb2415a715ec to your computer and use it in GitHub Desktop.
Save sturdysturge/cdef56d8484b931f016fbb2415a715ec to your computer and use it in GitHub Desktop.
//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