In Swift, how wide is Int, and why might a file-transfer model store its byte counts as Int64 rather than Int or UInt?
answer
- the platform's native word size
- arm64_32 watchOS builds
- same size is not same type
- prefer signed even when nonnegative
- Int128 needs a newer runtime
basics
~20 sSwift's Int matches the platform word size: 64 bits on iPhone, iPad and Mac, 32 bits on 32-bit targets such as arm64_32 watchOS. Int64 keeps byte counts above 2 GiB valid everywhere, and a signed type avoids UInt's conversions and subtraction traps.
solid answer
~40 s`Int` is the platform's word size: 64 bits on 64-bit targets, 32 bits on 32-bit ones such as watchOS on arm64_32, where `Int.max` is just under 2.15 billion, so a byte count over 2 GiB does not fit. `Int64` is 64 bits on every platform, which makes a transfer model correct everywhere and documents data that usually arrives as 64-bit anyway. Even on a 64-bit device `Int` and `Int64` are distinct types and need `Int64(x)`. `UInt` looks natural for counts that cannot be negative, but Swift's guidance prefers signed types: unsigned values force conversions wherever they meet other code, and `total - sent` going below zero traps instead of yielding a checkable negative. `Int128` and `UInt128` exist since Swift 6.0, behind `#available` on older deployment targets.
code
swift · 15 linestypealias ByteCount = Int64
struct FileTransfer {
var bytesSent: ByteCount = 0
let totalBytes: ByteCount
}
let video = FileTransfer(totalBytes: 4_831_838_208) // 4.5 GiB
print(Int.bitWidth, video.totalBytes > Int64(Int32.max)) // 64 true on a 64-bit Mac
print(0x10_0000, 0b1010, 0o17, 1.5e3) // 1048576 10 15 1500.0
if #available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *) {
let digest: UInt128 = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
print(digest == UInt128.max, Int128.bitWidth) // true 128
}go deeper
Recall that Int has the platform's word size and that Swift prefers Int, even for values that are never negative.
Explain why Int and Int64 are distinct types even at the same width, and why a fixed-width signed type suits byte counts.
Catch models that assume a 64-bit Int on a 32-bit watchOS build, and set sized types at the boundaries where external data arrives.
Decide which numeric widths a shared model layer standardises on, weighing portability across all shipped platforms against conversion noise at call sites.
## Int follows the platform's word size Swift's general-purpose integer, `Int`, has the **same size as the platform's native word**: - on a 64-bit platform `Int` is 64 bits wide, the same size as `Int64`, which covers current iPhone, iPad, Mac, Apple TV and Apple Vision Pro targets; - on a 32-bit platform it is 32 bits, the same size as `Int32`; the **arm64_32** architecture that some watchOS builds target is such a platform; - `UInt` follows the same rule as an unsigned type. Even on a 32-bit platform `Int` holds every value from -2,147,483,648 to 2,147,483,647, which is plenty for counts, indices and offsets. It is not plenty for a byte count: 2,147,483,647 bytes is just under 2 GiB, and a single video file can be larger. On arm64_32, `let bytes: Int = 3_000_000_000` is rejected at compile time with "integer literal '3000000000' overflows when stored into 'Int'", and a value that grows past the limit at run time traps. Same size does not mean same type. On a 64-bit Mac, `Int` and `Int64` are still **distinct types**, so passing an `Int` where an `Int64` is expected fails with "cannot convert value of type 'Int' to specified type 'Int64'"; you write `Int64(count)`. ## The sized integer family | Type | Width | Typical use | |---|---|---| | `Int` / `UInt` | platform word | counts, indices, everyday arithmetic | | `Int8` / `UInt8` | 8 bits | raw bytes (`UInt8`), compact protocol fields | | `Int16` / `UInt16` | 16 bits | audio samples, binary formats | | `Int32` / `UInt32` | 32 bits | C APIs, file-format fields | | `Int64` / `UInt64` | 64 bits | byte counts, timestamps, database IDs that must be 64-bit everywhere | | `Int128` / `UInt128` | 128 bits | values wider than 64 bits; added in Swift 6.0 | Each type has `min`, `max` and `bitWidth`, so `Int.bitWidth` answers the width question in code. `Int128` and `UInt128` came with Swift 6.0 and need the Swift 6.0 runtime: the Swift 6.4 compiler reports "'Int128' is only available in macOS 15.0 or newer" (and iOS 18.0 for an iOS target) when the deployment target is older, so code that supports earlier systems guards it with `#available`. ## Why Int64 for a file-transfer model, and not UInt A file-transfer model that stores `bytesSent` and `totalBytes` wants three things: a range large enough for files over 2 GiB, the **same** range on every platform it ships to, and easy arithmetic. That points to `Int64`: 1. `Int64` is 64 bits on every platform, so the same model code is correct on a 32-bit watchOS build and a 64-bit iPhone. 2. The explicit width documents the data, as Swift's guidance recommends for "explicitly sized data from an external source". 3. Byte counts from a server, a file system or a network layer are commonly 64-bit already, so no narrowing conversion is needed. `UInt` or `UInt64` look attractive because byte counts are never negative, but Swift's guidance is to prefer `Int` (or a signed sized type) **even for nonnegative values**: unsigned types force a conversion at every point where they meet the rest of the code, and a subtraction such as `total - sent` that goes below zero traps instead of producing a negative number you could check. Overflow behaviour and the wrapping operators belong with Swift's operators. Use unsigned types for bit patterns and raw bytes, not for "this number happens to be positive". ## typealias and literals make the intent readable A **type alias** gives the choice a domain name without creating a new type: `typealias ByteCount = Int64`. Values of `ByteCount` and `Int64` are interchangeable, so the alias documents intent but does not stop someone passing an unrelated `Int64`; a wrapper struct does that. Numeric literals help too: - underscores group digits and are ignored: `4_831_838_208`, `1_048_576`; - prefixes select a base: `0x10_0000` (hexadecimal), `0b1010` (binary), `0o17` (octal); - floating-point literals take an exponent: `1.5e3` is `1500.0`, and a hexadecimal float such as `0x1p10` needs its `p` exponent. A literal written for a byte count, such as `0x10_0000` for one mebibyte, reads better than `1048576` and is checked against the destination type's range at compile time.
- On a 64-bit Mac, can you pass an Int where a Swift function expects an Int64?No. They have the same width there, but they are distinct types and Swift has no implicit numeric conversion, so the compiler reports "cannot convert value of type 'Int' to specified type 'Int64'". Write `Int64(count)`; it always succeeds because every `Int` fits in an `Int64`.
- When would you reach for Swift's Int128 or UInt128?When a value genuinely needs more than 64 bits: a 128-bit identifier or hash, an intermediate product of two 64-bit values, or a C API using a 128-bit integer. They arrived in Swift 6.0 and need the Swift 6.0 runtime, so on a deployment target older than macOS 15 or iOS 18 they sit behind `#available`.
- Int is always 64 bits in Swift, on every Apple platform.
- On 64-bit devices Int and Int64 are the same type and mix freely.
- Byte counts are never negative, so UInt is the idiomatic Swift choice.
- Int128 works on any deployment target because it is built into the language.
- A typealias ByteCount = Int64 stops callers from passing a plain Int64.
Assumes Swift 6.4; Int128 and UInt128 were added in Swift 6.0 and need macOS 15 or iOS 18 and later at run time.