Why is [NSString hash] device dependent?

纵饮孤独 提交于 2019-12-11 05:08:53

问题


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



来源:https://stackoverflow.com/questions/20690659/why-is-nsstring-hash-device-dependent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!