Why is [NSString hash] device dependent?

2019-08-04 04:56发布

问题:

I was not expecting that hash on NSString returns differently depending on the target device.

NSLog(@"%lu", (unsigned long)[@"test" hash]);
// 38178019076 on my iPad.
// 3818280708 on my iPhone and (iPhone/iPad-)Simulator.

Apart from the fact that hash on NSString should be used with care (hash is bad with NSString) and this problem can easily be solved using a real hash (e.g. sha), I am interested why different results are returned?

回答1:

The NSObject protocol hash method returns an NSUInteger. NSUInteger is of varied type based on the architecture of the device (32 or 64 bit). It's probably that the hash implementation is different for 64 bit devices versus 32 bit devices (using the additional space to make more accurate hashes).