0

I'm trying to extend a Swift array to add a method containsValue which will return a bool if a given value exists.

extension Array
{

    func containsValue(value: T) -> Bool
    {
        for item in self
        {
            if item == value
            {
                return true
            }
        }
    }

}

But this gives the following error on the if item == value line.

Cannot invoke == with an argument list of type (T, T)

How can I compare the given values in a generic way so I can add this extensions to my array?

edit: I found a global function contains which does the trick. But I am still curious, how could I achieve this with the generics?

Community
  • 1
  • 1
Matthijn
  • 3,126
  • 9
  • 46
  • 69

0 Answers0