ios Swift 中的 Int 和 Int32 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27440100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Int and Int32 in Swift?
提问by János
In Core Data
you can store Int16
, Int32
, Int64
but it is different from Int
. What is the reason for their existence, how do you use them?
在Core Data
您可以存储Int16
, Int32
,Int64
但它不同于Int
。它们存在的原因是什么,你如何使用它们?
回答by J?rgen R
According to the Swift Documentation
根据Swift 文档
Int
In most cases, you don't need to pick a specific size of integer to use in your code. Swift provides an additional integer type, Int, which has the same size as the current platform's native word size:
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.
整数
在大多数情况下,您不需要选择特定大小的整数来在您的代码中使用。Swift 提供了一个额外的整数类型 Int,它的大小与当前平台的原生字大小相同:
在 32 位平台上,Int 与 Int32 的大小相同。
在 64 位平台上,Int 的大小与 Int64 相同。
除非您需要使用特定大小的整数,否则请始终在代码中使用 Int 作为整数值。这有助于代码一致性和互操作性。即使在 32 位平台上,Int 也可以存储 -2,147,483,648 和 2,147,483,647 之间的任何值,并且对于许多整数范围来说都足够大。
回答by C1FR1
The number placed after the "Int" is a reference to how many bits it uses. When you are using an int for something really short or almost unnecessary, like a running count of how many objects in a list that follow a specific parameter, I normally use an UInt8, which is an integer that has a maximum value of 256 (2^8), and a minimum value of 0, because it is unsigned (this is the difference between UInt8 and Int8). If it is signed, (or doesn't have a "U" prefixing the variable) then it can be negative. The same is true for Int16, Int32, and Int64. The bonus for using a smaller sized Int type is not very large, so you don't really need to use these if you don't want to.
“Int”后面的数字是它使用多少位的参考。当您将 int 用于非常短或几乎不必要的事情时,例如对列表中特定参数后面的对象数量进行运行计数时,我通常使用 UInt8,它是一个整数,最大值为 256 (2 ^8),最小值为 0,因为它是无符号的(这是 UInt8 和 Int8 之间的区别)。如果它是有符号的(或变量前没有“U”),那么它可以是负数。Int16、Int32 和 Int64 也是如此。使用较小尺寸的 Int 类型的好处不是很大,所以如果你不想的话,你真的不需要使用它们。
回答by Mehdi Abderezai
There is no performance savings if you are running on a laptop or iOS device with a 32 bit or 64 bit processor. Just use Int. The CPU doesn't just use 8 bits when you use Int8, the CPU will use its entire bit width regardless of what you use, because the hardware is already in the chip...
如果您在配备 32 位或 64 位处理器的笔记本电脑或 iOS 设备上运行,则不会节省性能。只需使用 Int。当您使用 Int8 时,CPU 不只是使用 8 位,无论您使用什么,CPU 都会使用其整个位宽,因为硬件已经在芯片中了...
Now if you have an 8 bit CPU, then using Int32 would require the compiler to do a bunch of backflips and magic tricks to get the 32 bit Int to work
现在如果你有一个 8 位 CPU,那么使用 Int32 将需要编译器做一堆后空翻和魔术来让 32 位 Int 工作
回答by yoAlex5
Swift's Int
is a wrapper because it has the same sizeas a platform capacity (Int32
on 32-bit platform and Int64
on 64-bit platform).
SwiftInt
是一个包装器,因为它具有与平台容量相同的大小(Int32
在 32 位平台和Int64
64 位平台上)。
As a programmer, you should not declare a platform dependent Int (e.g. Int32, Int64) unless you really need it. For example, when you are working on 32-bit platform with numbers that cannot be represented using 4 bytes, then you can declare Int64
instead of Int
.
作为程序员,除非确实需要,否则不应声明依赖于平台的 Int(例如 Int32、Int64)。例如,当您在 32 位平台上使用无法使用 4 个字节表示的数字时,您可以声明Int64
而不是Int
.
Int32
: 4 bytes: from ?2147483648 to +2147483647Int64
: 8 bytes: from ?9223372036854775808 to +9223372036854775807
Int32
:4 个字节:从 ?2147483648 到 +2147483647Int64
: 8 个字节:从 ?9223372036854775808 到 +9223372036854775807