Created
January 14, 2021 19:24
-
-
Save krzyzanowskim/377380fb3821f14fca23afedefcac722 to your computer and use it in GitHub Desktop.
Swift class Equatable need check parent class
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
class A: Equatable { | |
let name: String | |
init(name: String) { | |
self.name = name | |
} | |
static func ==(lhs: A, rhs: A) -> Bool { | |
return lhs.name == rhs.name | |
} | |
} | |
class B: A { | |
let identifier: String | |
init(name: String, identifier: String) { | |
self.identifier = identifier | |
super.init(name: name) | |
} | |
static func ==(lhs: B, rhs: B) -> Bool { | |
(lhs as A) == (rhs as A) && // !!! this is important | |
lhs.identifier == rhs.identifier | |
} | |
} | |
let b1 = B(name: "one", identifier: "three") | |
let b2 = B(name: "ones", identifier: "three") | |
print(b1 == b2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment