Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Created July 6, 2016 10:31
Show Gist options
  • Save KentarouKanno/ff507e15f139e62c2b60dbcac0d0cadc to your computer and use it in GitHub Desktop.
Save KentarouKanno/ff507e15f139e62c2b60dbcac0d0cadc to your computer and use it in GitHub Desktop.
Array Custom

Array Custom

★ 独自クラスで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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment