Identity
A micro-package that provides a way to uniquely identify objects, especially
value types.
This package provides two types: UniqueID
, and UniqueIdentifiable
.
The only requirement to conform to UniqueIdentifiable
is to implement an id
property of the type UniqueID
.
struct Moose: UniqueIdentifiable {
let id = UniqueID()
}
The UniqueID
type functions similarly to a UUID, in that it generates itself
upon its initialization. It uses a cryptographically secure pseudo-random number
generator to ensure its uniqueness, and is often faster than the UUID
type in
Foundation.
You can use the id
property in the type’s Equatable
and Hashable
implementations, like so:
extension Moose: Equatable {
static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.id == rhs.id
}
}
extension Moose: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}