Created
February 23, 2020 07:45
-
-
Save plantpurecode/1f546acd058d41de7d9f1fabc3c2355a to your computer and use it in GitHub Desktop.
A neat way to avoid nil checks with collections...
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
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