Skip to content

Instantly share code, notes, and snippets.

@plantpurecode
Created February 23, 2020 07:45
Show Gist options
  • Save plantpurecode/1f546acd058d41de7d9f1fabc3c2355a to your computer and use it in GitHub Desktop.
Save plantpurecode/1f546acd058d41de7d9f1fabc3c2355a to your computer and use it in GitHub Desktop.
A neat way to avoid nil checks with collections...
let array = ["a", "b", "c", "d"].map(Optional.init)
array.contains("a") // true
array.contains(Optional("a")) // true
array.contains("e") // false
array.contains(Optional("e")) // false
let otherArray = ["a"]
// `Array.first` is an Optional property, but we can use it as an argument to `Array.contains` since the array is actually of Optional<String> type...
array.contains(otherArray.first) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment