Skip to content

Instantly share code, notes, and snippets.

@aryaxt
Created November 1, 2016 04:20
Show Gist options
  • Save aryaxt/01214d72a4e4dcb6cebd2e243cd5f829 to your computer and use it in GitHub Desktop.
Save aryaxt/01214d72a4e4dcb6cebd2e243cd5f829 to your computer and use it in GitHub Desktop.
Swift Identifiable protocol
// 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