0

I'm trying to construct a 'contains' function that return true or false depending on if the array contains the parameter supplied in object.

I get this error: Can't invoke 'exist' with argument list of type '((T) -> Bool)'.

I can't tell if the problem is connected with the extension of Array, or it is in the closures. Both expressions in contains() have similar error.

The error looks ridiculous since ((T) -> Bool) is exactly what I have written for parameter of 'exist'

extension Array {    
    func exist(requirement:(T) -> Bool) -> Bool{
        for a in self{
            if(requirement(a)){
                return true
            }
        }
        return false
    }
    func contains(a:T) -> Bool {
        //return exist{return $0==a}
        return exist{(s0:T)->Bool in return s0==a}
    }
}
Cesare
  • 9,139
  • 16
  • 78
  • 130
  • The problem is that the `contains()` method requires the element type `T` to be `Equatable`, so that you can compare `s0 == a`. But Swift does not allow to define an array extension which applies only to a restricted type. See also http://stackoverflow.com/questions/24938948/array-extension-to-remove-object-by-value and http://stackoverflow.com/questions/24938948/array-extension-to-remove-object-by-value. – Martin R May 09 '15 at 12:02
  • Thank you. I got confused by the error and it does not suggest anything related to that. – user3468762 May 09 '15 at 14:55
  • Yes, the Swift error messages can be *very* confusing and misleading. I hope that the referenced threads answer your question. – Martin R May 09 '15 at 14:56

0 Answers0