0

I'm implementing the -hash method on a class, which is supposed to return an NSUInteger. My implementation for it was going to be similar to this:

- (NSUInteger) hash {
    return CFHash(self->cfObj);
}

Now, CFHash returns a CFHashCode, which is a typedefed unsigned long, but as far as I'm aware an NSUInteger is either an unsigned long OR an unsigned int.

What sort of problems could I run into if I'm returning an unsigned long when the application expects an unsigned int?

jscs
  • 63,694
  • 13
  • 151
  • 195
dark_perfect
  • 1,458
  • 1
  • 23
  • 41
  • I think there won't be any problems, even if there are hash collisions, they should be handled properly. – Felix Mar 26 '12 at 11:59

1 Answers1

3

What platform are you compiling for? As you pointed out, an NSUInteger is either an unsigned long or unsigned int depending on whether or not your architecture is 32 or 64 bit. For 64-bit OS X apps, this is obviously fine.

I think you should be safe even on iOS or 32-bit OS X apps. The docs describe the return type of CFHash:

An integer of type CFHashCode that represents a hashing value for cf.

That's not a compelling reason, but the default implementation of hash apparently uses CFHash anyway.

Ash Furrow
  • 12,391
  • 3
  • 57
  • 92
  • I'm compiling for ios, but I'm not sure whether this was 32 or 64-bit. My main worry was that returning a long would cause some sort of overflow on 32-bit systems, but from your answer, this isn't something I have to worry about? – dark_perfect Mar 26 '12 at 15:42
  • You should be fine - iOS is 32-bit, but you shouldn't get any collisions using `CFHash` with integers. – Ash Furrow Mar 26 '12 at 21:41