★ 独自クラスでcontains()
やindexOf()
を使えるようにする
class EquatableArray: Equatable {
var name: String
init(_ name: String) {
self.name = name
}
}
func == (l: EquatableArray, r: EquatableArray) -> Bool {
return l.name == r.name
}
let apple = EquatableArray("Apple")
let banana = EquatableArray("Banana")
let orange = EquatableArray("Orange")
var items = [apple, banana, orange]
let found = items.contains(apple) // true
let index = items.indexOf(banana) // 1
let index = items.indexOf(Item("Orange")) // 2