> > My question is, apple equals banana, but their hashValues (in their own > types) don't. What's wrong here?
Hi Zhao. In addition to what Jordan and Dmitri have said, I think part of the confusion is that you are assuming hash values are implicitly used in an equality check. They are not. They are used when your instances are added to certain types of collections. In your first example, where you print out the hash values but then compare lhs.name to rhs.name, the names of the two fruits are both "common fruit", and the equality test returns true. Hash never comes into play. You can test for yourself when the hash gets used: import Foundation class Foo: NSObject { override var hash: Int { print("Computing hash value!") return 1 } } var f1 = Foo() var f2 = Foo() f1 == f2 // Doesn't print anything! var aSet = Set<Foo>() aSet.insert(f1) // Prints "Computing has value!" Lou
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users