What is a correct way to specify Swift.Int: %d or %lld?

This year’s WWDC session named “What’s New in Swift” has got the following sample code:


let quantity = 10
let formatString = NSLocalizedString(
  "You have %lld apples",
  comment: "Number of apples"
)
label.text = String(format: formatString, quantity)


This is the first time ever when I meet “%lld” as a specifier for an integer value.


So what is a recommended specifier for the “Swift.Int” type? Is it “%d” or “%lld” and why? Thanks in advance!

Accepted Reply

The format specifiers used in `String.init(format:)` are derived from `[NSString format:]` (or C's *

`printf`) and described in this doc.


Generally, `ll` stands for `long long`, which represents 64-bit integer in Apple's platforms. (Both in 32-bit and 64-bit.)

And in Swift, `long long` is equivalent to `Int64` (or `CLongLong`).


So, use `%lld` for `Int64`.


And `%d` takes C-`int`, which is equivalent to `Int32` (or `CInt`) in Swift. (Always represents 32-bit, both in 32-bit and 64-bit platforms.)


`Swift.Int` is equivalent to C-`long` (or `long int` or `NSInteger`), which is 32-bit in 32-bit platforms and 64-bit in 64-bit platforms.

And the right format for C-`long` is `%ld`.


If you write your code only for 64-bit platforms, `%lld` and `%ld` are equivalent, but I recommend you to use `%ld` for `Swift.Int`.

Replies

The format specifiers used in `String.init(format:)` are derived from `[NSString format:]` (or C's *

`printf`) and described in this doc.


Generally, `ll` stands for `long long`, which represents 64-bit integer in Apple's platforms. (Both in 32-bit and 64-bit.)

And in Swift, `long long` is equivalent to `Int64` (or `CLongLong`).


So, use `%lld` for `Int64`.


And `%d` takes C-`int`, which is equivalent to `Int32` (or `CInt`) in Swift. (Always represents 32-bit, both in 32-bit and 64-bit platforms.)


`Swift.Int` is equivalent to C-`long` (or `long int` or `NSInteger`), which is 32-bit in 32-bit platforms and 64-bit in 64-bit platforms.

And the right format for C-`long` is `%ld`.


If you write your code only for 64-bit platforms, `%lld` and `%ld` are equivalent, but I recommend you to use `%ld` for `Swift.Int`.

Thanks a lot for the detailed reply 👍