Swift check if an element is in an array
Checking if an element is an an array in swift is easy, with no special code required. In the example below we search our array the number “3” and print out “yes” if it is found:
let elements = [3, 6, 9, 1, 7, 9] if elements.contains(3) { print("yes") }
Since our array does in fact contain the number 3, it will print out “yes” to the console. This can be used for objects other then numbers like the integers in the example above. Strings, booleans and other data types can be used to see if the array contains the value.