Swift 2 Array Contains object?

后端 未结 4 2011
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  逝去的感伤
    2021-02-20 19:26

    Your Dog needs to implement Equatable.

    class Dog: Equatable {
    
       var age = 1
    
    }
    
    func == (lhs: Dog, rhs: Dog) -> Bool {
          return lhs.age == rhs.age
    }
    

提交回复
热议问题