Created
November 1, 2016 04:20
-
-
Save aryaxt/01214d72a4e4dcb6cebd2e243cd5f829 to your computer and use it in GitHub Desktop.
Swift Identifiable protocol
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
// This allows you to implement identifiable on any class/struct and automatially make them Equatable | |
protocol Identifiable: Equatable { | |
var id: Int { get } | |
} | |
func == <T: Identifiable>(lhs: T, rhs: T) -> Bool { | |
return lhs.id == rhs.id | |
} | |
struct User: Identifiable { | |
let id: Int | |
} | |
let u1 = User(id: 1) | |
let u2 = User(id: 2) | |
u1 == u2 | |
[u1, u2].contains(u1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment