Can I cast Int64 directly into Int? Can I cast Int64 directly into Int? ios ios

Can I cast Int64 directly into Int?

Converting an Int64 to Int by passing the Int64 value to the Int initializer will always work on a 64-bit machine, and it will crash on a 32-bit machine if the integer is outside of the range Int32.min ... Int32.max.

For safety use the init(truncatingIfNeeded:) initializer (formerly known as init(truncatingBitPattern:) in earlier Swift versions) to convert the value:

return Int(truncatingIfNeeded: rowid)

On a 64-bit machine, the truncatingIfNeeded will do nothing; you will just get an Int (which is the same size as an Int64 anyway).

On a 32-bit machine, this will throw away the top 32 bits, but it they are all zeroes, then you haven't lost any data. So as long as your value will fit into a 32-bit Int, you can do this without losing data. If your value is outside of the range Int32.min ... Int32.max, this will change the value of the Int64 into something that fits in a 32-bit Int, but it will not crash.

You can see how this works in a Playground. Since Int in a Playground is a 64-bit Int, you can explicitly use an Int32 to simulate the behavior of a 32-bit system.

let i: Int64 = 12345678901  // value bigger than maximum 32-bit Intlet j = Int32(truncatingIfNeeded: i)  // j = -539,222,987let k = Int32(i)                        // crash!

Update for Swift 3/4

In addition to init(truncatingIfNeeded:) which still works, Swift 3 introduces failable initializers to safely convert one integer type to another. By using init?(exactly:) you can pass one type to initialize another, and it returns nil if the initialization fails. The value returned is an optional which must be unwrapped in the usual ways.

For example:

let i: Int64 = 12345678901if let j = Int32(exactly: i) {    print("\(j) fits into an Int32")} else {    // the initialization returned nil    print("\(i) is too large for Int32")}

This allows you to apply the nil coalescing operator to supply a default value if the conversion fails:

// return 0 if rowid is too big to fit into an Int on this devicereturn Int(exactly: rowid) ?? 0

If you're confident that the Int64 value can be represented exactly as an Int, use Int(truncatingIfNeeded:), e.g.:

let salary: Int64 = 100000let converted = Int(truncatingIfNeeded: salary)

For builds targeting 32-bit devices, the range for Int is limited to -2147483648 through 2147483647, the same as Int32. Values outside of that range will quietly have their high-order bits discarded. This results in garbage, often of the opposite sign.

If the value might be out of range, and you want to handle that condition, use Int(exactly:), e.g.:

if let converted = Int(exactly: salary) {    // in range    ... converted ...} else {    // out-of-range    ...}

In the specific case of rowids, using Int64 rather than Int was a deliberate API design choice, and truncating to Int could be a bug.

It's a pain in the ass. It's monumentally stupider than in Objective-C. But here is how you do it.

If you are using a UInt64, you do it like this.

let thatVarYouWantToConvert: UInt64 = 1let myInt: Int = Int(exactly:thatVarYouWantToConvert ?? 0)

If you are using a UInt64?, you do it like this.

let thatVarYouWantToConvert: UInt64? = 1let myInt: Int = Int(exactly:thatVarYouWantToConvert ?? 0) ?? 0

Like I said, "It's monumentally stupider than in Objective-C."

Tested in Swift 4.2, Xcode 10.2.1