Cannot find an overload for 'contains' that accepts an argument list of type in Swift

时间:2015-07-08 15:45:16

标签: swift contains

I want to check the similar object from my custom array in swift. I am using contains method but i am getting an error.

var companies:[Company] = [Company]()
var company = Company()

if !contains(self.companies,company) //I am having an error here
{
    ...
}

I get this error:

Error: Cannot find an overload for 'contains' that accepts an argument list of type '([Company], Company)'

2 个答案:

答案 0 :(得分:3)

作为解决方法 - 强制转换为NSArray

(companies as NSArray).containsObject(company)

答案 1 :(得分:1)

Maybe you just need Company to be Equatable. Something like this:

struct Company: Equatable {

}

func ==(lhs: Company, rhs: Company) -> Bool {
    return true // Do your custom implementation that returns true if lhs and hrs should be considered equal
}

Hope this helps