Skip to content

Instantly share code, notes, and snippets.

@kvs-coder
Created October 31, 2021 08:27
Show Gist options
  • Save kvs-coder/d84f1288b6d05561e1c30868f3bd1680 to your computer and use it in GitHub Desktop.
Save kvs-coder/d84f1288b6d05561e1c30868f3bd1680 to your computer and use it in GitHub Desktop.
Reflection Hashable
class MyHashable: Hashable {
var dumped: String {
var str = String()
dump(self, to: &str)
return str
}
static func == (lhs: MyHashable, rhs: MyHashable) -> Bool {
lhs.dumped == rhs.dumped
}
func hash(into hasher: inout Hasher) {
Mirror(reflecting: self).children
.map { $0.value }
.withUnsafeBytes { hasher.combine(bytes: $0) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment