Integer literal overflows when stored into 'Int'
UInt32(0x8BC34AFF)
creates a UInt32
by calling an initializer. The UInt32
initializer you are calling is:
init(_ v: Int)
The problem is that on a 32-bit device (iPhone5 and earlier), type Int
is 32-bits. So, the constant you are passing 0x8BC34AFF
overflows the Int
that you are passing to the initializer of UInt32
.
The way to have this work on both 32-bit and 64-bit devices is to cast the integer literal to the type:
let primary = 0x8BC34AFF as UInt32
Alternatively, declare the variable to be UInt32
and just assign the constant:
let primary:UInt32 = 0x8BC34AFF
How to fix the Integer literal '2147483648' overflows when stored into 'Int' exception?
As @OOPer noted in the comments, on a 32-bit system Int
is 32-bits and your value is larger than Int32.max
. Since you are decoding 4 bytes you can use UInt32
:
func checkHexToUInt32(stringData: String) -> UInt32? {
return UInt32(stringData, radix: 16)
}
let stringData = "84121516" // this is 4 bytes data
let value = self.checkHexToUInt32(stringData: stringData)
let checkEngineLightOn = ((value! & 0x80000000) > 0)
Note: UInt32(_:radix:)
returns an UInt32?
which is nil
if the conversion fails, so there is no reason for the guard
and return nil
, just return the value of the conversion.
Integer literal '4294967295' overflows when stored into 'Int'
The compiler considers 0xFFFFFFFF
as an integer literal, but the
value is too large for a (signed) Int
on a 32-bit platform.
Choosing the (unsigned) UInt32
instead solves the problem:
func random() -> CGFloat{
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
Integer literal overflows when stored into 'Int' error
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)
Integer literal '255' overflows when stored into 'Int8'
func decodeIDArrayItem(index:Int, tokenArray:UnsafeMutablePointer<CChar>) -> UInt32{
var value:UInt32 = UInt32(tokenArray[index * 4]) & 0xFF
value <<= 8
value |= UInt32(tokenArray [index * 4 + 1]) & 0xFF
value <<= 8
value |= UInt32(tokenArray [index * 4 + 2]) & 0xFF
value <<= 8
value |= UInt32(tokenArray [index * 4 + 3]) & 0xFF
return value
}
Integer overflows when stored into 'Int Phone Number
This is expected and a correct error. Why are you putting the number in \()
? That evaluates it as Swift. As Swift that's a literal number, which is too big to fit in an Int. You almost certainly mean this:
"tel://8708382937"
Or more sensibly:
"tel:8708382937"
(The slashes are specifically part of the HTTP URL-scheme. They are not a general part of URLs and do not belong on tel
URLs.)
Firebase server timestamp and non 64 bit device: Integer literal '...' overflows when stored into 'Int'
You can explicitly deal with larger numbers, even on 32 bit devices, if you explicitly specify UInt64
or Int64
(unsigned, and signed, respectively).
Related Topics
How to Perform Face Detection in Swift
Type of Expression Is Ambiguous Without More Context in Xcode 11
Generic Method Override Not Working in Swift
Swiftui Share Sheet Crashes iPad
Metal Kernels Not Behaving Properly on the New MACbook Pro (Late 2016) Gpus
Does Swift Optimise Chained Creation and Copy of Structs
Custom Uitabbar Unselected Item's Color
Iwatch: Wkinterfacelabel How to Stop Text from Being Cut Off with "..." at The End of a Label
Sharing Highscore with Social Media
Zoom to Fit Current Location and Annotation on Map
How to Open Your App's Settings (Inside the Settings App) with Swift (iOS 11)
Fullscreen for Swift Playgrounds on iPad
Ios8 Swift Uisearchcontroller Hides Navigationbar
Sktexture Nearest Filtering Mode Doesn't Work (Making Pixel Art)
Swift Generics Protocols: Can Only Be Used as a Generic Constraint Problem