How to get the index of an object in an array using index(of:)

The index(of:) tells you the index of an element in an array if it exists. Else it will return nil, therefore index(of:) is an optional method.


If your array is of a custom class or struct then you can also find the index of this element using a closure as shown below.