How do i avoid a program crash, when I cast a UInt16(value over 32767) to Int16.
In Object-C is easy and safe:
uint16_t a = 32888
int16_t b = (int16_t)a
But I don't have any idea in Swift.
How do i avoid a program crash, when I cast a UInt16(value over 32767) to Int16.
In Object-C is easy and safe:
uint16_t a = 32888
int16_t b = (int16_t)a
But I don't have any idea in Swift.
You can use the following function:
func toInt(unsigned: UInt) -> Int {
let signed = (unsigned <= UInt(Int.max)) ?
Int(unsigned) :
Int(unsigned - UInt(Int.max) - 1) + Int.min
return signed
}