簡體   English   中英

不同設備上int類型的大小不同

[英]Different size of int type on different devices

考慮以下代碼片段:

if (((int)[@"foo" rangeOfString @"a"].location+1) > 0)
{
    // found a
}
else
{
  // not found a
}

在發行版本中,它可以在更新的設備(例如iPad Air)上正常運行(即轉到//未找到)。 但是在諸如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會丟失精度和/或符號。

我相信它可能與32位和64位體系結構有關,也許優化器正在弄亂它

打印出您正在使用的值

NSInteger是32或64位。 int是32位。 那應該解釋一切。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM