Skip to content

Instantly share code, notes, and snippets.

@mediym41
Last active June 26, 2022 19:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mediym41/6cc741485f0903c3dc23bf7004853757 to your computer and use it in GitHub Desktop.
Save mediym41/6cc741485f0903c3dc23bf7004853757 to your computer and use it in GitHub Desktop.
Easily compare two objects by using KeyPath
final class KeyPathEqualizer<Object> {
private let lhs: Object
private let rhs: Object
private var areEqual: Bool = true
init(lhs: Object, rhs: Object) {
self.lhs = lhs
self.rhs = rhs
}
func compare<T: Equatable>(by keyPath: KeyPath<Object, T>) -> Self {
guard areEqual else {
return self
}
if lhs[keyPath: keyPath] != rhs[keyPath: keyPath] {
areEqual = false
}
return self
}
func build() -> Bool {
return areEqual
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment