Swift 2 Array Contains object?

后端 未结 4 1900
隐瞒了意图╮
隐瞒了意图╮ 2021-02-20 18:44

Why isn\'t this working? I can use array.contains() on a String but it doesn\'t work for an Object.

var array = [\"A\", \"B\", \"C\"]

array.contains(\"A\") //          


        
4条回答
  •  Happy的楠姐
    2021-02-20 19:43

    To really explain what's happening there, first we have to understand there are two contains methods on Array (or better said, on SequenceType).

    func contains(_ element: Self.Generator.Element) -> Bool
    

    with constraints

    Generator.Element : Equatable
    

    and

    func contains(@noescape _ predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
    

    The first one basically searches for a given element in the array using ==. The second one uses a closure that returns a Bool to search for elements.

    The first method cannot be used because Dog doesn't adopt Equatable. The compiler tries to use the second method but that one has a closure as the parameter, hence the error you are seeing.

    Solution: implement Equatable for Dog.

    If you are looking for object reference comparison, you can use a simple closure:

    let result = dogs.contains({ $0 === sparky })
    

提交回复
热议问题