objective c - help with isEquals and hash in iphone -


so i'm overriding isequals , hash compare custom objects able remove duplicates nsarray. problem i'm missing values in list contains no duplicated items, , seems hash or isequals implementation wrong. custom object course object has variables like: id , name i'll put code here:

- (bool)isequal:(id)object {     if ([object iskindofclass:[course self]]) {         return yes;     }      if(self == object){         return yes;     }     else {         return no;     } }  - (unsigned)hash {      nsstring *idhash = [nsstring stringwithformat: @"%d", self._id];     return [idhash hash]; } 

then, after querying database put values in array , in set should remove duplicated items this:

nsmutableset *noduplicates = [[nsmutableset alloc] initwitharray:tempresults]; 

can see i'm doing wrong in isequals or hash implementation?

thanks lot.

this how implement hash , isequal (at-least 1 working me purpose of identifying duplicates)

hash function

the apple doc says hash of 2 objects should same considered equal( logically). hence implement hash below

-(unsigned int)hash   {       return 234;//some random constant   }  

isequal: method implemenation like

-(bool)isequal:(id)otherobject   {     myclass *thisclassobj = (myclass*)otherobject;    *// replaced condition statement proves logically 2 object same if 2 different instances*   return ([thisclassobj primarykey] == [self primarykey]);  } 

more reference here : techniques implementing -hash on mutable cocoa objects

implementing -hash / -isequal: / -isequalto...: objective-c collections


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -