<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">In Objective-C, it says</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="georgia, serif">If two objects are equal, they must have the same hash value. This last point is particularly important if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure you also define hash in your subclass.</font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html#//apple_ref/occ/intfm/NSObject/isEqual:">https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/index.html#//apple_ref/occ/intfm/NSObject/isEqual:</a> </blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">​So the conclusion is that hash can be override.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">However, in Swift, things go a little odd. Below code works in both Swift 2.2(Xcode 7.3.1) and Swift 3.0(Xcode 8 beta 6).</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo">





</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Fruit:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Hashable</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">0</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;common fruit&quot;</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> = name</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">enum</span><span style="font-variant-ligatures:no-common-ligatures"> FruitShape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> small = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1000</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> medium = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">2000</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> big = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">3000</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Apple:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">medium</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">override</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">5</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Banana:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">big</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">override</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">10</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> apple = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> banana = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">apple</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">==</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">banana</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures">/*</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> 5</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> 10</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> true</span></p>
<div class="gmail_default" style="font-family:georgia,serif"><span style="color:rgb(0,132,0);font-family:menlo;font-size:11px">*/</span></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><font color="#000000" face="georgia, serif">My question is, apple equals banana, but their hashValues (in their own types)  don&#39;t. What&#39;s wrong here? Is that means we shouldn&#39;t override hashValue in subclass in Swift?</font></div><div class="gmail_default"><font color="#000000" face="georgia, serif"><br></font></div><div class="gmail_default"><font color="#000000" face="georgia, serif">Zhaoxin</font></div></div>