Techniques for implementing -hash on mutable Cocoa objects

后端 未结 6 572
终归单人心
终归单人心 2020-12-31 10:06

The documentation for -hash says it must not change while a mutable object is stored in a collection, and similarly the documentation for -isEqual:

6条回答
  •  别那么骄傲
    2020-12-31 10:31

    Since you are already overriding -isEqual: to do a value-based comparison, are you sure you really need to bother with -hash?

    I can't guess what exactly you need this for of course, but if you want to do value-based comparison without deviating from the expected implementation of -isEqual: to only return YES when hashes are identical, a better approach might be to mimick NSString's -isEqualToString:, so to create your own -isEqualToFoo: method instead of using or overriding -isEqual:.

提交回复
热议问题