Search code examples
objective-cxcodereinterpret-cast

How can we reinterpret a double or float as NSUInteger to create hash?


This is my isEqual and hash custom operator

- (BOOL)isEqual:(id)object;
{
    BGSearchParameter * theOther = (BGSearchParameter *)object;

    BOOL isTheOtherEqual;
    isTheOtherEqual = isTheOtherEqual && [self.Location isEqual:theOther.Location];
    isTheOtherEqual = isTheOtherEqual && [self.keyword isEqual:theOther.keyword];
    isTheOtherEqual = isTheOtherEqual && (self.Distance == theOther.Distance);
    isTheOtherEqual = isTheOtherEqual && (self.SortByWhat == theOther.SortByWhat);
    isTheOtherEqual = isTheOtherEqual && (self.startFrom == theOther.startFrom);
    isTheOtherEqual = isTheOtherEqual && (self.numberOfIDstoGrab == theOther.numberOfIDstoGrab);

    return isTheOtherEqual;
}
- (NSUInteger)hash
{
    NSUInteger returnValue=0;
    returnValue ^= self.Location.hash;
    returnValue ^= self.keyword.hash;

    return returnValue;
}

That one does the job. However, say I want to incorporate the distance and the startfrom into the hash.

I guess I will simply add:

returnValue ^= self.Distance;

It's an error because it's not compatible.

So what should I do instead?


Solution

  • I ended up turning the number into NSNumber and got the hash:

       returnValue ^= @(self.Distance).hash;
       returnValue ^= @(self.SortByWhat).hash;
       returnValue ^= @(self.startFrom).hash;
       returnValue ^= @(self.numberOfIDstoGrab).hash;
    

    Martin answer is good. However, the result should be the same anyway and I don't want to implement another complicated function.