为什么[NSString hash]设备依赖? [英] Why is [NSString hash] device dependent?

查看:112
本文介绍了为什么[NSString hash]设备依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没想到NSString上的哈希值会根据目标设备的不同而不同.

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.

除了应谨慎使用NSString上的哈希(使用NSString哈希很不好),可以使用真实哈希(例如sha)轻松解决此问题,我有兴趣为什么会返回不同的结果?

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?

推荐答案

NSObject协议hash方法返回NSUInteger. NSUInteger的类型取决于设备的体系结构(32或64位).对于64位设备和32位设备,散列实现可能有所不同(使用额外的空间进行更准确的散列).

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

这篇关于为什么[NSString hash]设备依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆