Reputation: 29519
Having
let finalValueUnsigned64:UInt64
I would like to convert it to Int64
let finalValue = Int64(finalValueUnsigned64)
But the initializers doesn't throw exception so I can handle the situations, when the value is too large and conversion is not possible. How can I convert any Unsigned to Signed, with some feedback whether conversion is possible or not?
Upvotes: 2
Views: 81
Reputation: 56635
You can use the Int64(exactly:)
initializer to check if a value can be converted or not. If the value can't be represented exactly it will return nil
. For example:
Int8(exactly: 100) // Optional(100)
Int8(exactly: 1_000) // nil
Upvotes: 3