Ramifications of returning an unsigned long when unsigned int is expected
-
26-05-2021 - |
Domanda
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 typedef
ed 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
?
Soluzione
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.