考虑以下代码片段:
if (((int)[@"foo" rangeOfString @"a"].location+1) > 0)
{
// found a
}
else
{
// not found a
}
在发布版本中,它在 iPad Air 等较新的设备上运行良好(即转到//not found a)。但在 iPad 2 等旧设备上,它不会(即转到//找到一个)。
当通过 Xcode 调试时,它在所有设备上都能正常工作。
PS:我知道以上是糟糕的编码习惯,我应该使用以下内容。但我试图理解上述行为。
if ([@"foo" rangeOfString @"a"].location != NSNotFound)
{
// found a
}
else
{
// not found a
}
最佳答案
rangeOfString.location
如果字符串不包含子字符串,则返回 NSNotFound
。
NSNotFound
声明为 NSIntegerMax
,在 32 位系统上为 32 位,在 64 位系统上为 64 位。
通过将类型强制转换为始终为 32 位的 int
会出现问题。
将 64 位整数转换为 int
会丢失精度和/或符号。
关于ios - 不同设备上 int 类型的大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092188/