Checking if an array of custom objects contain a specific custom object

There are two contains functions:

extension SequenceType where Generator.Element : Equatable {
    /// Return `true` iff `element` is in `self`.
    public func contains(element: Self.Generator.Element) -> Bool

extension SequenceType {
    /// Return `true` iff an element in `self` satisfies `predicate`.
    public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool

The compiler is complaining because the compiler knows that Person is not Equatable and thus contains needs to have a predicate but alex is not a predicate.

If the people in your array are Equatable (they aren’t) then you could use:


Since they aren’t equatable, you could use the second contains function with:

person.list.contains { $ == }

or, as Martin R points out, based on ‘identity’ with:

person.list.contains { $0 === alex }

or you could make Person be Equatable (based on either name or identity).

Leave a Comment