Swift Array contains()

The contains() method checks whether the specified element is present in the array or not.

Example


contains() Syntax

The syntax of the array contains() method is:

array.contains(obj)

Here, array is an object of the Array class.


contains() Parameters

The contains() method takes a single parameter:

  • obj - element to be checked for its presence in the array

contains() Return Values

The contains() method returns:

  • true - if the array contains the specified element
  • false - if the array doesn't contain the specified element

Example 1: Swift String contains()

Output

true
false

In the above example,

  • "Nadal" is present in the array, so the method returns true.
  • "Federer" is not present in the array, so the method returns false.

Example 2: Using contains() With if...else

Output

Array contains Gregory
Array doesn't contain gregory