简体   繁体   English

为什么= 32在32位与64位iOS设备上表现不同?

[英]Why does == behave different on 32bit vs 64bit iOS devices?

I know in Objective-C == is very different from isEqual: . 我知道Objective-C ==isEqual:非常不同isEqual: But I am curious about why the following code produces consistently different results on 32bit vs 64bit iOS devices. 但我很好奇为什么以下代码在32位与64位iOS设备上产生一致的不同结果。

NSIndexPath* a = [NSIndexPath indexPathForItem:0 inSection:0];
NSIndexPath* b = [NSIndexPath indexPathForItem:0 inSection:0];
NSLog(@"%@", a == b ? @"YES" : @"NO");
NSLog(@"%@", [a isEqual:b] ? @"YES" : @"NO");

On 32 bit device, eg iPhone 5, == always fails (expected). 在32位设备上,例如iPhone 5, ==总是失败(预期)。

2016-12-07 09:55:18.019 NSIndexPathTestObjc[18667:1958831] NO
2016-12-07 09:55:18.020 NSIndexPathTestObjc[18667:1958831] YES

On 64 bit device, eg iPhone 6s, == succeeds (unexpected). 在64位设备上,例如iPhone 6s, ==成功(意外)。

2016-12-07 09:56:05.503 NSIndexPathTestObjc[18780:1960472] YES
2016-12-07 09:56:05.505 NSIndexPathTestObjc[18780:1960472] YES

I know in Objective-C == is very different from isEqual: 我知道Objective-C ==与isEqual非常不同:

Yes -- you should use the latter to compare two objects. 是的 - 您应该使用后者来比较两个对象。 == only tells you whether the pointers you're comparing are the same. ==仅告诉您比较的指针是否相同。

But I am curious about why the following code produces consistently different results on 32bit vs 64bit iOS devices. 但我很好奇为什么以下代码在32位与64位iOS设备上产生一致的不同结果。

This is probably just be an implementation detail of -indexPathForItem:inSection: , where it might cache index paths that it generates on 64-bit systems and returns the cached object the second time you call the method with the same item, but doesn't on 32-bit systems. 这可能只是-indexPathForItem:inSection:的实现细节-indexPathForItem:inSection: ,它可能会缓存它在64位系统上生成的索引路径,并在第二次使用相同项调用方法时返回缓存对象,但不会在32位系统上。 Don't rely on this behavior: use -isEqual: for object comparison. 不要依赖于这种行为:使用-isEqual:进行对象比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM