Created
April 11, 2016 20:18
-
-
Save mikekavouras/ff1eaea47585cb477860dd63919ac1af to your computer and use it in GitHub Desktop.
uni
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //: Playground - noun: a place where people can play | |
| import UIKit | |
| struct Obj { | |
| let id: String | |
| init(id: String) { | |
| self.id = id | |
| } | |
| } | |
| extension Obj: Equatable {} | |
| func ==(lhs: Obj, rhs: Obj) -> Bool { | |
| return lhs.id == rhs.id | |
| } | |
| extension Array where Element: Equatable{ | |
| var unique: [Element] { | |
| var uniqueValues: [Element] = [] | |
| forEach { item in | |
| if !uniqueValues.contains(item) { | |
| uniqueValues += [item] | |
| } | |
| } | |
| return uniqueValues | |
| } | |
| } | |
| let one = Obj(id: "beep") | |
| let two = Obj(id: "beep") | |
| let three = Obj(id: "boop") | |
| var arr = [one, two, three] | |
| var uniq: [Obj] = arr.unique | |
| print(uniq) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment