Techniques for implementing -hash on mutable Cocoa objects












12














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



Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0)? Does anybody know how the mutable versions of framework-provided classes do this?



The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.



EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.










share|improve this question
























  • guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
    – nielsbot
    Mar 16 '12 at 3:24










  • @nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
    – Lily Ballard
    Mar 16 '12 at 4:29










  • CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
    – nielsbot
    Mar 16 '12 at 8:36










  • @nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
    – Lily Ballard
    Mar 16 '12 at 19:06










  • yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
    – nielsbot
    Mar 17 '12 at 3:43
















12














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



Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0)? Does anybody know how the mutable versions of framework-provided classes do this?



The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.



EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.










share|improve this question
























  • guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
    – nielsbot
    Mar 16 '12 at 3:24










  • @nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
    – Lily Ballard
    Mar 16 '12 at 4:29










  • CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
    – nielsbot
    Mar 16 '12 at 8:36










  • @nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
    – Lily Ballard
    Mar 16 '12 at 19:06










  • yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
    – nielsbot
    Mar 17 '12 at 3:43














12












12








12


4





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



Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0)? Does anybody know how the mutable versions of framework-provided classes do this?



The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.



EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.










share|improve this question















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



Given this, does anybody have any suggestions for the best way to implement -hash such that it meets both these conditions and yet is actually calculated intelligently (i.e. doesn't just return 0)? Does anybody know how the mutable versions of framework-provided classes do this?



The simplest thing to do is of course just forget the first condition (about it not changing) and just make sure I never accidentally mutate an object while it's in a collection, but I'm wondering if there's any solution that's more flexible.



EDIT: I'm wondering here whether it's possible to maintain the 2 contracts (where equal objects have equal hashes, and hashes don't change while the object is in a collection) when I'm mutating the internal state of the object. My inclination is to say "no", unless I do something stupid like always return 0 for the hash, but that's why I'm asking this question.







objective-c cocoa cocoa-touch hash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 '09 at 15:56

























asked Jan 14 '09 at 12:40









Lily Ballard

147k23327315




147k23327315












  • guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
    – nielsbot
    Mar 16 '12 at 3:24










  • @nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
    – Lily Ballard
    Mar 16 '12 at 4:29










  • CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
    – nielsbot
    Mar 16 '12 at 8:36










  • @nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
    – Lily Ballard
    Mar 16 '12 at 19:06










  • yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
    – nielsbot
    Mar 17 '12 at 3:43


















  • guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
    – nielsbot
    Mar 16 '12 at 3:24










  • @nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
    – Lily Ballard
    Mar 16 '12 at 4:29










  • CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
    – nielsbot
    Mar 16 '12 at 8:36










  • @nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
    – Lily Ballard
    Mar 16 '12 at 19:06










  • yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
    – nielsbot
    Mar 17 '12 at 3:43
















guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
– nielsbot
Mar 16 '12 at 3:24




guess this is an old question, just found it... but aren't mutable objects used as keys in a collection usually copied? Doesn't that just sidestep the problem?
– nielsbot
Mar 16 '12 at 3:24












@nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
– Lily Ballard
Mar 16 '12 at 4:29




@nielsbot: Only keys for NSDictionaries are copied. NSSet doesn't copy its objects, and the CFDictionarySetValue() API doesn't copy its keys either.
– Lily Ballard
Mar 16 '12 at 4:29












CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
– nielsbot
Mar 16 '12 at 8:36




CFDictionarySetValue does if you pass kCFTypeDictionaryKeyCallbacks to CFDictionaryCreate, no? The docs are almost non-sensical... I suppose a mutable collection obj could, uh, cache the hash values, which is the same as assuming a mutable obj in a collection will not change its hash, right?
– nielsbot
Mar 16 '12 at 8:36












@nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
– Lily Ballard
Mar 16 '12 at 19:06




@nielsbot: kCFTypeDictionaryKeyCallbacks only retains the key. -[NSMutableDictionary setObject:forKey:] is a bit special in that it copies the incoming key, even if it's really a CFMutableDictionaryRef that has a key callbacks that doesn't retain the key.
– Lily Ballard
Mar 16 '12 at 19:06












yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
– nielsbot
Mar 17 '12 at 3:43




yeah.. you're right. I guess you could replace the retain callback with a copy callback... but maybe that's skanky. ...ANYways.... /me back to work
– nielsbot
Mar 17 '12 at 3:43












6 Answers
6






active

oldest

votes


















3














Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.



In this case, you have 2 choices, neither of which works:




  1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.

  2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.


It seems to me that there's no possible way to do this without using a constant as a hashcode.






share|improve this answer























  • That's what I thought, and I guess it's probably right.
    – Lily Ballard
    Jan 14 '09 at 16:37



















2














My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."



To quote directly from the NSObject documentation for hash:




If a mutable object is added to a
collection that uses hash values to
determine the object’s position in the
collection, the value returned by the
hash method of the object must not
change while the object is in the
collection. Therefore, either the hash
method must not rely on any of the
object’s internal state information or
you must make sure the object’s
internal state information does not
change while the object is in the
collection
.




(Emphasis mine.)






share|improve this answer























  • Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
    – Lily Ballard
    Jan 14 '09 at 14:52










  • If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
    – Evan DiBiase
    Jan 14 '09 at 15:11










  • On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
    – Evan DiBiase
    Jan 14 '09 at 15:14










  • I'm asking the former. I'll update the question to make this clearer
    – Lily Ballard
    Jan 14 '09 at 15:55



















1














The question here isn't how to meet both of these requirements, but rather which one you should meet. In Apple's documentation, it is clearly stated that:




a mutable dictionary can be put in a hash table but you must not change it while it is in there.




This being said, it seems more important that you meet the equality requirement of hashes. The hash of an object should always be a way to check if an object is equal to another. If this is ever not the case, it is not a true hash function.



Just to finish up my answer, I'll give an example of a good hash implementation. Let's say you are writing the implementation of -hash on a collection that you have created. This collection stores an array of NSObjects as pointers. Since all NSObjects implement the hash function, you can use their hashes in calculating the collection's hash:



- (NSUInteger)hash {
NSUInteger theHash = 0;
for (NSObject * aPtr in self) { // fast enumeration
theHash ^= [aPtr hash];
}
return theHash;
}


This way, two collection objects containing the same pointers (in the same order) will have the same hash.






share|improve this answer





























    0














    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:.






    share|improve this answer

















    • 1




      The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
      – Quinn Taylor
      Jul 10 '09 at 22:36



















    -1














    The answer to this question and the key to avoiding many cocoa-bugs is this:



    Read the documentation carefully. Place every word and punctuation on a golden scale and weight it as it was the world's last grain of wheat.



    Let's read the documentation again:




    If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, [...]




    (emphasis mine).



    What the writer of the docs, in his/hers eternal wisdom, mean by this is that when you are implementing a collection, like a dictionary, you shouldn't use the hash for positioning since that can change. In other words it has little to do with implementing -hash on mutable Cocoa objects (which all of us thought it had, assuming the documentation has not changed in the last ~10 years since the question was asked).



    That is why dictionaries always copy their keys - so they can guarantee
    that the hash value won't change.



    You will then ask the question: But, good sir, how does NSMapTable and similar handle this?



    The answer to this is according to the documentation:




    "Its keys or values may be copied on input or may use pointer identity for equality and hashing."




    (emphasis mine again).



    Since we were so easily fooled by the documentation last time, let's run a little experiment to see for ourselves how stuff actually work:



    NSMutableString *string = [NSMutableString stringWithString:@"so lets mutate this"];
    NSString *originalString = string.copy;
    NSMapTable *mutableStrings = [NSMapTable strongToStrongObjectsMapTable];
    [mutableStrings setObject:originalString forKey:string];

    [string appendString:@" into a larger string"];
    if ([mutableStrings objectForKey:string] == nil)
    NSLog(@"not found!");

    if ([mutableStrings objectForKey:originalString] == nil)
    NSLog(@"Not even the original string is found?");

    for (NSString *inCollection in mutableStrings)
    {
    NSLog(@"key '%@' : is '%@' (null)", inCollection, [mutableStrings objectForKey:inCollection]);
    }
    for (NSString *value in NSAllMapTableValues(mutableStrings))
    {
    NSLog(@"value exists: %@", value);
    }


    Surprise!



    So, instead of using pointer equality, they focus on the words "may" here which in this case mean "may not", and simply copy the hash value when adding stuff to the collection.



    (All this is actually good, since it would be quite difficult to implement NSHashMap, or -hash, otherwise).






    share|improve this answer































      -2














      In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.






      share|improve this answer

















      • 2




        Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
        – Lily Ballard
        Jan 14 '09 at 13:04











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f442808%2ftechniques-for-implementing-hash-on-mutable-cocoa-objects%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.



      In this case, you have 2 choices, neither of which works:




      1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.

      2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.


      It seems to me that there's no possible way to do this without using a constant as a hashcode.






      share|improve this answer























      • That's what I thought, and I guess it's probably right.
        – Lily Ballard
        Jan 14 '09 at 16:37
















      3














      Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.



      In this case, you have 2 choices, neither of which works:




      1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.

      2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.


      It seems to me that there's no possible way to do this without using a constant as a hashcode.






      share|improve this answer























      • That's what I thought, and I guess it's probably right.
        – Lily Ballard
        Jan 14 '09 at 16:37














      3












      3








      3






      Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.



      In this case, you have 2 choices, neither of which works:




      1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.

      2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.


      It seems to me that there's no possible way to do this without using a constant as a hashcode.






      share|improve this answer














      Interesting question, but I think what you want is logically impossible. Say you start with 2 objects, A and B. They're both different, and they start with different hash codes. You add both to some hash table. Now, you want to mutate A, but you can't change the hash code because it's already in the table. However, it's possible to change A in such a way that it .equals() B.



      In this case, you have 2 choices, neither of which works:




      1. Change the hashcode of A to equal B.hashcode, which violates the constraint of not changing hash codes while in a hash table.

      2. Don't change the hashcode, in which case A.equals(B) but they don't have the same hashcodes.


      It seems to me that there's no possible way to do this without using a constant as a hashcode.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 14 '09 at 16:54









      Chris Hanson

      49.3k667103




      49.3k667103










      answered Jan 14 '09 at 16:11









      Outlaw Programmer

      8,15473859




      8,15473859












      • That's what I thought, and I guess it's probably right.
        – Lily Ballard
        Jan 14 '09 at 16:37


















      • That's what I thought, and I guess it's probably right.
        – Lily Ballard
        Jan 14 '09 at 16:37
















      That's what I thought, and I guess it's probably right.
      – Lily Ballard
      Jan 14 '09 at 16:37




      That's what I thought, and I guess it's probably right.
      – Lily Ballard
      Jan 14 '09 at 16:37













      2














      My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."



      To quote directly from the NSObject documentation for hash:




      If a mutable object is added to a
      collection that uses hash values to
      determine the object’s position in the
      collection, the value returned by the
      hash method of the object must not
      change while the object is in the
      collection. Therefore, either the hash
      method must not rely on any of the
      object’s internal state information or
      you must make sure the object’s
      internal state information does not
      change while the object is in the
      collection
      .




      (Emphasis mine.)






      share|improve this answer























      • Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
        – Lily Ballard
        Jan 14 '09 at 14:52










      • If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
        – Evan DiBiase
        Jan 14 '09 at 15:11










      • On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
        – Evan DiBiase
        Jan 14 '09 at 15:14










      • I'm asking the former. I'll update the question to make this clearer
        – Lily Ballard
        Jan 14 '09 at 15:55
















      2














      My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."



      To quote directly from the NSObject documentation for hash:




      If a mutable object is added to a
      collection that uses hash values to
      determine the object’s position in the
      collection, the value returned by the
      hash method of the object must not
      change while the object is in the
      collection. Therefore, either the hash
      method must not rely on any of the
      object’s internal state information or
      you must make sure the object’s
      internal state information does not
      change while the object is in the
      collection
      .




      (Emphasis mine.)






      share|improve this answer























      • Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
        – Lily Ballard
        Jan 14 '09 at 14:52










      • If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
        – Evan DiBiase
        Jan 14 '09 at 15:11










      • On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
        – Evan DiBiase
        Jan 14 '09 at 15:14










      • I'm asking the former. I'll update the question to make this clearer
        – Lily Ballard
        Jan 14 '09 at 15:55














      2












      2








      2






      My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."



      To quote directly from the NSObject documentation for hash:




      If a mutable object is added to a
      collection that uses hash values to
      determine the object’s position in the
      collection, the value returned by the
      hash method of the object must not
      change while the object is in the
      collection. Therefore, either the hash
      method must not rely on any of the
      object’s internal state information or
      you must make sure the object’s
      internal state information does not
      change while the object is in the
      collection
      .




      (Emphasis mine.)






      share|improve this answer














      My reading of the documentation is that a mutable object's value for hash can (and probably should) change when it is mutated, but should not change when the object hasn't been mutated. The portion of the documentation to which to refer, therefore, is saying, "Don't mutate objects that are stored in a collection, because that will cause their hash value to change."



      To quote directly from the NSObject documentation for hash:




      If a mutable object is added to a
      collection that uses hash values to
      determine the object’s position in the
      collection, the value returned by the
      hash method of the object must not
      change while the object is in the
      collection. Therefore, either the hash
      method must not rely on any of the
      object’s internal state information or
      you must make sure the object’s
      internal state information does not
      change while the object is in the
      collection
      .




      (Emphasis mine.)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 14 '09 at 14:18

























      answered Jan 14 '09 at 14:02









      Evan DiBiase

      984816




      984816












      • Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
        – Lily Ballard
        Jan 14 '09 at 14:52










      • If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
        – Evan DiBiase
        Jan 14 '09 at 15:11










      • On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
        – Evan DiBiase
        Jan 14 '09 at 15:14










      • I'm asking the former. I'll update the question to make this clearer
        – Lily Ballard
        Jan 14 '09 at 15:55


















      • Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
        – Lily Ballard
        Jan 14 '09 at 14:52










      • If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
        – Evan DiBiase
        Jan 14 '09 at 15:11










      • On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
        – Evan DiBiase
        Jan 14 '09 at 15:14










      • I'm asking the former. I'll update the question to make this clearer
        – Lily Ballard
        Jan 14 '09 at 15:55
















      Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
      – Lily Ballard
      Jan 14 '09 at 14:52




      Yes, that's the way I read it, but if the hash doesn't rely on internal state that makes it pretty hard to make equal objects hash the same. This is why I'm asking this question, to find out if anybody has a clever solution to storing mutable objects in map tables.
      – Lily Ballard
      Jan 14 '09 at 14:52












      If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
      – Evan DiBiase
      Jan 14 '09 at 15:11




      If you're looking for clever tricks for this, I don't think I can help, but note that the documentation says that the hash method must not rely on internal state or the internal state must be guaranteed not to change while in the collection. You can use internal state if you conform to the latter.
      – Evan DiBiase
      Jan 14 '09 at 15:11












      On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
      – Evan DiBiase
      Jan 14 '09 at 15:14




      On re-reading the question: are you asking if there's a way to keep the hash and isEqual: contract while mutating an object in a collection, or just whether it's possible to keep the contract in general? I was responding to the second question, which may not be what you were asking.
      – Evan DiBiase
      Jan 14 '09 at 15:14












      I'm asking the former. I'll update the question to make this clearer
      – Lily Ballard
      Jan 14 '09 at 15:55




      I'm asking the former. I'll update the question to make this clearer
      – Lily Ballard
      Jan 14 '09 at 15:55











      1














      The question here isn't how to meet both of these requirements, but rather which one you should meet. In Apple's documentation, it is clearly stated that:




      a mutable dictionary can be put in a hash table but you must not change it while it is in there.




      This being said, it seems more important that you meet the equality requirement of hashes. The hash of an object should always be a way to check if an object is equal to another. If this is ever not the case, it is not a true hash function.



      Just to finish up my answer, I'll give an example of a good hash implementation. Let's say you are writing the implementation of -hash on a collection that you have created. This collection stores an array of NSObjects as pointers. Since all NSObjects implement the hash function, you can use their hashes in calculating the collection's hash:



      - (NSUInteger)hash {
      NSUInteger theHash = 0;
      for (NSObject * aPtr in self) { // fast enumeration
      theHash ^= [aPtr hash];
      }
      return theHash;
      }


      This way, two collection objects containing the same pointers (in the same order) will have the same hash.






      share|improve this answer


























        1














        The question here isn't how to meet both of these requirements, but rather which one you should meet. In Apple's documentation, it is clearly stated that:




        a mutable dictionary can be put in a hash table but you must not change it while it is in there.




        This being said, it seems more important that you meet the equality requirement of hashes. The hash of an object should always be a way to check if an object is equal to another. If this is ever not the case, it is not a true hash function.



        Just to finish up my answer, I'll give an example of a good hash implementation. Let's say you are writing the implementation of -hash on a collection that you have created. This collection stores an array of NSObjects as pointers. Since all NSObjects implement the hash function, you can use their hashes in calculating the collection's hash:



        - (NSUInteger)hash {
        NSUInteger theHash = 0;
        for (NSObject * aPtr in self) { // fast enumeration
        theHash ^= [aPtr hash];
        }
        return theHash;
        }


        This way, two collection objects containing the same pointers (in the same order) will have the same hash.






        share|improve this answer
























          1












          1








          1






          The question here isn't how to meet both of these requirements, but rather which one you should meet. In Apple's documentation, it is clearly stated that:




          a mutable dictionary can be put in a hash table but you must not change it while it is in there.




          This being said, it seems more important that you meet the equality requirement of hashes. The hash of an object should always be a way to check if an object is equal to another. If this is ever not the case, it is not a true hash function.



          Just to finish up my answer, I'll give an example of a good hash implementation. Let's say you are writing the implementation of -hash on a collection that you have created. This collection stores an array of NSObjects as pointers. Since all NSObjects implement the hash function, you can use their hashes in calculating the collection's hash:



          - (NSUInteger)hash {
          NSUInteger theHash = 0;
          for (NSObject * aPtr in self) { // fast enumeration
          theHash ^= [aPtr hash];
          }
          return theHash;
          }


          This way, two collection objects containing the same pointers (in the same order) will have the same hash.






          share|improve this answer












          The question here isn't how to meet both of these requirements, but rather which one you should meet. In Apple's documentation, it is clearly stated that:




          a mutable dictionary can be put in a hash table but you must not change it while it is in there.




          This being said, it seems more important that you meet the equality requirement of hashes. The hash of an object should always be a way to check if an object is equal to another. If this is ever not the case, it is not a true hash function.



          Just to finish up my answer, I'll give an example of a good hash implementation. Let's say you are writing the implementation of -hash on a collection that you have created. This collection stores an array of NSObjects as pointers. Since all NSObjects implement the hash function, you can use their hashes in calculating the collection's hash:



          - (NSUInteger)hash {
          NSUInteger theHash = 0;
          for (NSObject * aPtr in self) { // fast enumeration
          theHash ^= [aPtr hash];
          }
          return theHash;
          }


          This way, two collection objects containing the same pointers (in the same order) will have the same hash.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 18 '11 at 3:14









          Alex Nichol

          6,73442627




          6,73442627























              0














              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:.






              share|improve this answer

















              • 1




                The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
                – Quinn Taylor
                Jul 10 '09 at 22:36
















              0














              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:.






              share|improve this answer

















              • 1




                The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
                – Quinn Taylor
                Jul 10 '09 at 22:36














              0












              0








              0






              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:.






              share|improve this answer












              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:.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 15 '09 at 8:53









              Dirk Stoop

              2,8521618




              2,8521618








              • 1




                The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
                – Quinn Taylor
                Jul 10 '09 at 22:36














              • 1




                The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
                – Quinn Taylor
                Jul 10 '09 at 22:36








              1




              1




              The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
              – Quinn Taylor
              Jul 10 '09 at 22:36




              The NSObject protocol docs for this explicitly state that overriding -isEqual: and not -hash is incorrect, and can cause problems when an instance of the class is stored in a collection. Also, NSString overrides -isEqual: to call -isEqualToString: if both objects are strings. See this Apple doc: developer.apple.com/documentation/Cocoa/Conceptual/…
              – Quinn Taylor
              Jul 10 '09 at 22:36











              -1














              The answer to this question and the key to avoiding many cocoa-bugs is this:



              Read the documentation carefully. Place every word and punctuation on a golden scale and weight it as it was the world's last grain of wheat.



              Let's read the documentation again:




              If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, [...]




              (emphasis mine).



              What the writer of the docs, in his/hers eternal wisdom, mean by this is that when you are implementing a collection, like a dictionary, you shouldn't use the hash for positioning since that can change. In other words it has little to do with implementing -hash on mutable Cocoa objects (which all of us thought it had, assuming the documentation has not changed in the last ~10 years since the question was asked).



              That is why dictionaries always copy their keys - so they can guarantee
              that the hash value won't change.



              You will then ask the question: But, good sir, how does NSMapTable and similar handle this?



              The answer to this is according to the documentation:




              "Its keys or values may be copied on input or may use pointer identity for equality and hashing."




              (emphasis mine again).



              Since we were so easily fooled by the documentation last time, let's run a little experiment to see for ourselves how stuff actually work:



              NSMutableString *string = [NSMutableString stringWithString:@"so lets mutate this"];
              NSString *originalString = string.copy;
              NSMapTable *mutableStrings = [NSMapTable strongToStrongObjectsMapTable];
              [mutableStrings setObject:originalString forKey:string];

              [string appendString:@" into a larger string"];
              if ([mutableStrings objectForKey:string] == nil)
              NSLog(@"not found!");

              if ([mutableStrings objectForKey:originalString] == nil)
              NSLog(@"Not even the original string is found?");

              for (NSString *inCollection in mutableStrings)
              {
              NSLog(@"key '%@' : is '%@' (null)", inCollection, [mutableStrings objectForKey:inCollection]);
              }
              for (NSString *value in NSAllMapTableValues(mutableStrings))
              {
              NSLog(@"value exists: %@", value);
              }


              Surprise!



              So, instead of using pointer equality, they focus on the words "may" here which in this case mean "may not", and simply copy the hash value when adding stuff to the collection.



              (All this is actually good, since it would be quite difficult to implement NSHashMap, or -hash, otherwise).






              share|improve this answer




























                -1














                The answer to this question and the key to avoiding many cocoa-bugs is this:



                Read the documentation carefully. Place every word and punctuation on a golden scale and weight it as it was the world's last grain of wheat.



                Let's read the documentation again:




                If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, [...]




                (emphasis mine).



                What the writer of the docs, in his/hers eternal wisdom, mean by this is that when you are implementing a collection, like a dictionary, you shouldn't use the hash for positioning since that can change. In other words it has little to do with implementing -hash on mutable Cocoa objects (which all of us thought it had, assuming the documentation has not changed in the last ~10 years since the question was asked).



                That is why dictionaries always copy their keys - so they can guarantee
                that the hash value won't change.



                You will then ask the question: But, good sir, how does NSMapTable and similar handle this?



                The answer to this is according to the documentation:




                "Its keys or values may be copied on input or may use pointer identity for equality and hashing."




                (emphasis mine again).



                Since we were so easily fooled by the documentation last time, let's run a little experiment to see for ourselves how stuff actually work:



                NSMutableString *string = [NSMutableString stringWithString:@"so lets mutate this"];
                NSString *originalString = string.copy;
                NSMapTable *mutableStrings = [NSMapTable strongToStrongObjectsMapTable];
                [mutableStrings setObject:originalString forKey:string];

                [string appendString:@" into a larger string"];
                if ([mutableStrings objectForKey:string] == nil)
                NSLog(@"not found!");

                if ([mutableStrings objectForKey:originalString] == nil)
                NSLog(@"Not even the original string is found?");

                for (NSString *inCollection in mutableStrings)
                {
                NSLog(@"key '%@' : is '%@' (null)", inCollection, [mutableStrings objectForKey:inCollection]);
                }
                for (NSString *value in NSAllMapTableValues(mutableStrings))
                {
                NSLog(@"value exists: %@", value);
                }


                Surprise!



                So, instead of using pointer equality, they focus on the words "may" here which in this case mean "may not", and simply copy the hash value when adding stuff to the collection.



                (All this is actually good, since it would be quite difficult to implement NSHashMap, or -hash, otherwise).






                share|improve this answer


























                  -1












                  -1








                  -1






                  The answer to this question and the key to avoiding many cocoa-bugs is this:



                  Read the documentation carefully. Place every word and punctuation on a golden scale and weight it as it was the world's last grain of wheat.



                  Let's read the documentation again:




                  If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, [...]




                  (emphasis mine).



                  What the writer of the docs, in his/hers eternal wisdom, mean by this is that when you are implementing a collection, like a dictionary, you shouldn't use the hash for positioning since that can change. In other words it has little to do with implementing -hash on mutable Cocoa objects (which all of us thought it had, assuming the documentation has not changed in the last ~10 years since the question was asked).



                  That is why dictionaries always copy their keys - so they can guarantee
                  that the hash value won't change.



                  You will then ask the question: But, good sir, how does NSMapTable and similar handle this?



                  The answer to this is according to the documentation:




                  "Its keys or values may be copied on input or may use pointer identity for equality and hashing."




                  (emphasis mine again).



                  Since we were so easily fooled by the documentation last time, let's run a little experiment to see for ourselves how stuff actually work:



                  NSMutableString *string = [NSMutableString stringWithString:@"so lets mutate this"];
                  NSString *originalString = string.copy;
                  NSMapTable *mutableStrings = [NSMapTable strongToStrongObjectsMapTable];
                  [mutableStrings setObject:originalString forKey:string];

                  [string appendString:@" into a larger string"];
                  if ([mutableStrings objectForKey:string] == nil)
                  NSLog(@"not found!");

                  if ([mutableStrings objectForKey:originalString] == nil)
                  NSLog(@"Not even the original string is found?");

                  for (NSString *inCollection in mutableStrings)
                  {
                  NSLog(@"key '%@' : is '%@' (null)", inCollection, [mutableStrings objectForKey:inCollection]);
                  }
                  for (NSString *value in NSAllMapTableValues(mutableStrings))
                  {
                  NSLog(@"value exists: %@", value);
                  }


                  Surprise!



                  So, instead of using pointer equality, they focus on the words "may" here which in this case mean "may not", and simply copy the hash value when adding stuff to the collection.



                  (All this is actually good, since it would be quite difficult to implement NSHashMap, or -hash, otherwise).






                  share|improve this answer














                  The answer to this question and the key to avoiding many cocoa-bugs is this:



                  Read the documentation carefully. Place every word and punctuation on a golden scale and weight it as it was the world's last grain of wheat.



                  Let's read the documentation again:




                  If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, [...]




                  (emphasis mine).



                  What the writer of the docs, in his/hers eternal wisdom, mean by this is that when you are implementing a collection, like a dictionary, you shouldn't use the hash for positioning since that can change. In other words it has little to do with implementing -hash on mutable Cocoa objects (which all of us thought it had, assuming the documentation has not changed in the last ~10 years since the question was asked).



                  That is why dictionaries always copy their keys - so they can guarantee
                  that the hash value won't change.



                  You will then ask the question: But, good sir, how does NSMapTable and similar handle this?



                  The answer to this is according to the documentation:




                  "Its keys or values may be copied on input or may use pointer identity for equality and hashing."




                  (emphasis mine again).



                  Since we were so easily fooled by the documentation last time, let's run a little experiment to see for ourselves how stuff actually work:



                  NSMutableString *string = [NSMutableString stringWithString:@"so lets mutate this"];
                  NSString *originalString = string.copy;
                  NSMapTable *mutableStrings = [NSMapTable strongToStrongObjectsMapTable];
                  [mutableStrings setObject:originalString forKey:string];

                  [string appendString:@" into a larger string"];
                  if ([mutableStrings objectForKey:string] == nil)
                  NSLog(@"not found!");

                  if ([mutableStrings objectForKey:originalString] == nil)
                  NSLog(@"Not even the original string is found?");

                  for (NSString *inCollection in mutableStrings)
                  {
                  NSLog(@"key '%@' : is '%@' (null)", inCollection, [mutableStrings objectForKey:inCollection]);
                  }
                  for (NSString *value in NSAllMapTableValues(mutableStrings))
                  {
                  NSLog(@"value exists: %@", value);
                  }


                  Surprise!



                  So, instead of using pointer equality, they focus on the words "may" here which in this case mean "may not", and simply copy the hash value when adding stuff to the collection.



                  (All this is actually good, since it would be quite difficult to implement NSHashMap, or -hash, otherwise).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 10 at 20:02

























                  answered Nov 9 at 16:01









                  Olof_t

                  628721




                  628721























                      -2














                      In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.






                      share|improve this answer

















                      • 2




                        Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                        – Lily Ballard
                        Jan 14 '09 at 13:04
















                      -2














                      In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.






                      share|improve this answer

















                      • 2




                        Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                        – Lily Ballard
                        Jan 14 '09 at 13:04














                      -2












                      -2








                      -2






                      In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.






                      share|improve this answer












                      In Java, most mutable classes simply don’t override Object.hashCode() so that the default implementation returns a value that is based on the address of the object and doesn’t change. It might just be the same with Objective C.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 14 '09 at 12:47









                      Bombe

                      63.1k15108117




                      63.1k15108117








                      • 2




                        Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                        – Lily Ballard
                        Jan 14 '09 at 13:04














                      • 2




                        Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                        – Lily Ballard
                        Jan 14 '09 at 13:04








                      2




                      2




                      Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                      – Lily Ballard
                      Jan 14 '09 at 13:04




                      Except that violates the rule that equal objects need to have the same hash. The default implementation for -isEqual: just compares pointers, but I'm overriding it to do a value-based comparison of the fields in the object.
                      – Lily Ballard
                      Jan 14 '09 at 13:04


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f442808%2ftechniques-for-implementing-hash-on-mutable-cocoa-objects%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings