Damian
Damian

Reputation: 215

Integer literal overflows when stored into 'Int' error

I get an error message, when I archive my project. How to fix it?

static let litersPerUSGallon = NSDecimalNumber(mantissa:UInt64(3785411784), exponent: -9, isNegative:false)
static let litersPerImperialGallon = NSDecimalNumber(mantissa:UInt64(454609), exponent: -5, isNegative:false)
static let kilometersPerStatuteMile = NSDecimalNumber(mantissa:UInt64(1609344), exponent: -6, isNegative:false)
static let kilometersPerLiterToMilesPerUSGallon = NSDecimalNumber(mantissa:UInt64(2352145833), exponent: -9, isNegative:false)
static let kilometersPerLiterToMilesPerImperialGallon = NSDecimalNumber(mantissa:UInt64(2737067636), exponent: -9, isNegative:false)
static let litersPer100KilometersToMilesPer10KUSGallon = NSDecimalNumber(mantissa:UInt64(425170068027), exponent: -10, isNegative:false)
static let litersPer100KilometersToMilesPer10KImperialGallon = NSDecimalNumber(mantissa:UInt64(353982300885), exponent: -10, isNegative:false)

enter image description here

Upvotes: 1

Views: 4220

Answers (2)

Tom Coomer
Tom Coomer

Reputation: 6547

You can set the type to UInt64 by using the code below:

let myInt:UInt64 = 12345

Upvotes: 1

Martin R
Martin R

Reputation: 539815

During archiving, the code is compiled for all architectures which are configured in the build settings under "Architectures".

On the 32-bit iOS platforms, Int is a 32-bit signed integer which cannot hold the value 3785411784. It seems that the compiler cannot infer the type of the integer literal as UInt64 correctly in this context.

But the solution is simple: Just omit the UInt64() constructor. The type of the mantissa: parameter is UInt64, and the integer literal is correctly taken as a 64-bit number, even on 32-bit platforms.

NSDecimalNumber(mantissa:3785411784, exponent: -9, isNegative:false)

Upvotes: 5

Related Questions