gpt4 book ai didi

ios - iPhone 5 和 iPhone 6 中的不同 Int 类型

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:15 26 4
gpt4 key购买 nike

我正在将一个静态的整数流读入一个整数数组:

func create_int_array_from_nsmutable(nsdata:NSMutableData)  -> [Int] {
let pointer = UnsafePointer<Int>(nsdata.bytes)
let count = nsdata.length / sizeof(Int);
// Get buffer pointer and make an array out of it
let buffer = UnsafeBufferPointer<Int>(start:pointer, count:count)
let array = [Int](buffer)
return array;
}

现在我意识到,在 iPhone 5 中 Int 的大小为 4,而在 iPhone 6 或 iPad 中的大小为 8,这导致了完全不同的结果。

有人提示如何在两种情况下读取此流并获得相同的结果吗?

我应该使用 Int64 类型吗? Int64 是否正是因为这个原因而存在,例如解决兼容性问题?

最佳答案

关于 iPhone 和 iPad 的 Int 值类型大小不同的说法是错误的,32 位和 64 位处理器有这个区别。

来自Swift docs :

  • On a 32-bit platform, Int is the same size as Int32.
  • On a 64-bit platform, Int is the same size as Int64.

Unless you need to work with a specific size of integer, always use Int for integer values in your code. This aids code consistency and interoperability. Even on 32-bit platforms, Int can store any value between -2,147,483,648 and 2,147,483,647, and is large enough for many integer ranges.

如果您需要在多个架构中保持整数大小一致,请使用 Int32

关于ios - iPhone 5 和 iPhone 6 中的不同 Int 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35015918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com