Commit 62811e07 authored by Damien DeVille's avatar Damien DeVille

objc: also implement `hash` when implementing `isEqual:`

Apple docs mention that one should always override `hash` when
implementing equality and overriding `isEqual:`.
This is not the smartest hashing but better than nothing.
parent 435e88ca
......@@ -291,6 +291,33 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
}
w.wl(";")
}
w.wl
w.wl("- (NSUInteger)hash")
w.braced {
w.w(s"return ").nestedN(2) {
w.w(s"NSStringFromClass([self class]).hash")
for (f <- r.fields) {
w.wl(" ^")
f.ty.resolved.base match {
case MOptional =>
f.ty.resolved.args.head.base match {
case df: MDef if df.defType == DEnum =>
w.w(s"(NSUInteger)self.${idObjc.field(f.ident)}")
case _ => w.w(s"self.${idObjc.field(f.ident)}.hash")
}
case t: MPrimitive => w.w(s"(NSUInteger)self.${idObjc.field(f.ident)}")
case df: MDef => df.defType match {
case DEnum => w.w(s"(NSUInteger)self.${idObjc.field(f.ident)}")
case _ => w.w(s"self.${idObjc.field(f.ident)}.hash")
}
case _ => w.w(s"self.${idObjc.field(f.ident)}.hash")
}
}
}
w.wl(";")
}
w.wl
}
def generatePrimitiveOrder(ident: Ident, w: IndentWriter): Unit = {
......@@ -326,8 +353,8 @@ class ObjcGenerator(spec: Spec) extends Generator(spec) {
}
w.wl("return NSOrderedSame;")
}
}
w.wl
}
w.wl("@end")
})
}
......
......@@ -44,4 +44,17 @@
((self.oSixtyfour == nil && typedOther.oSixtyfour == nil) || (self.oSixtyfour != nil && [self.oSixtyfour isEqual:typedOther.oSixtyfour]));
}
- (NSUInteger)hash
{
return NSStringFromClass([self class]).hash ^
(NSUInteger)self.eight ^
(NSUInteger)self.sixteen ^
(NSUInteger)self.thirtytwo ^
(NSUInteger)self.sixtyfour ^
self.oEight.hash ^
self.oSixteen.hash ^
self.oThirtytwo.hash ^
self.oSixtyfour.hash;
}
@end
......@@ -39,5 +39,4 @@ DBConstants * __nonnull const DBConstantsObjectConstant = [[DBConstants alloc] i
return self;
}
@end
......@@ -25,6 +25,14 @@
return self.key1 == typedOther.key1 &&
[self.key2 isEqualToString:typedOther.key2];
}
- (NSUInteger)hash
{
return NSStringFromClass([self class]).hash ^
(NSUInteger)self.key1 ^
self.key2.hash;
}
- (NSComparisonResult)compare:(DBRecordWithDerivings *)other
{
NSComparisonResult tempResult;
......
......@@ -25,6 +25,14 @@
return self.key == typedOther.key &&
[self.rec isEqual:typedOther.rec];
}
- (NSUInteger)hash
{
return NSStringFromClass([self class]).hash ^
(NSUInteger)self.key ^
self.rec.hash;
}
- (NSComparisonResult)compare:(DBRecordWithNestedDerivings *)other
{
NSComparisonResult tempResult;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment