6

I got a couple user IDs I want to send in an array, but can't figure out the correct Swift 3 syntax for creating an array with very long integers. I tried casting, @ prefix and using as AnyObject, but that did not work.

let idArray = [10211420262370680, 10211420262370680]

Error: integer literal overflows when stored into int

What is the correct way to create an array with such long integers?

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Alex Stone
  • 46,408
  • 55
  • 231
  • 407

3 Answers3

18

Try this instead:

let idArray: [UInt64] = [10_211_420_262_370_680, ...]

As a back of the envelope calculation, every 10 bits buys you 3 decimal digits. For instance, UInt32 maxes out around 4_000_000_000 and so on.

By the way, the underscores _ above are just syntax sugar for big number literals ;-)

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
8

Signed long's array:

let signed64BitIntegerArray: [Int64] =      [-10211420262370680, 10211420262370680]

Unsigned long's array:

let unsigned64BitIntegerArray: [UInt64] =   [ 10211420262370680, 10211420262370680]
Roi Zakai
  • 252
  • 3
  • 3
0

If you need C interop/FFI, use CLong or CUnsignedLong.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45