swift - What is the pattern for entities and Equatable? -
suppose have base class:
class entity: equatable { init() { } var localid: int32? var id: int32? } func == (lhs: entity, rhs: entity) -> bool { return object_getclassname(lhs) == object_getclassname(rhs) && lhs.localid != nil && rhs.localid != nil && lhs.localid == rhs.localid } and number of entity implementations along lines of this:
class message: entity { init(senderid: int32, body: string, sentdatetime: nsdate) { self.senderid = senderid self.body = body self.sentdatetime = sentdatetime super.init() } var senderid: int32 var body: string var sentdatetime: nsdate } am taking right approach equatable implementation? reason compare class names uniqueness of localid scoped each entity type. should instead implement equatable each entity class? there established pattern in swift kind of thing?
the first big question whether localid , id have optional. , if entity does, seems more natural protocol rather superclass. , it's not clear why senderid etc. var rather let. these separate issues main question.
to equality check.
i don't believe need != nil checks here. should ok (at least in tests):
func == (lhs: entity, rhs: entity) -> bool { return object_getclassname(lhs) == object_getclassname(rhs) && lhs.localid == rhs.localid } but don't see problem basic idea here.
(you'd think use lhs.dynamictype == rhs.dynamictype, surprisingly (to me) doesn't compile currently.)
Comments
Post a Comment