iphone - do I really have to override hash just because I override isEqual: for my subclass? -


apple's document says if override isequal: have override hash make sure hash value should same 2 objects consider equal isequal:

then read docs hash , below part of it:

therefore, either hash method must not rely on of object’s internal state information or must make sure object’s internal state information not change while object in collection.

my customize class myclass have few of members int , bool , nsarray contains number of myclass , want 2 instance of myclass equal if of members equal.

i have no problem how override isequal: hash. of understanding, hash should calculate hash value combine members' hash value using bit operation such xor or rotational shift.

the problem how implement hash in such way meets apple's requirement mention @ above. docs says hash value should not rely internal state(which members) found have use them compute value.

or need implement it? because sure not use class key nsdictionary , way know hash used. there other places hash used , should care it?

there's 2 options - either don't rely on internal state, or ensure internal state doesn't change while object in collection.

the second option allow rely on internal state generate hash, object must immutable when in collection, changing doesn't change hash.

collections in cocoa rely on hash of object perform methods such containsobject:.

if object implements hash relies on internal state, inserted collection , changed, hash change, , collection lose track of object.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -