Post not yet marked as solved
Post marked as unsolved with 7 replies, 687 views
I have a swift callback function that accumulates data from the gyroscope handler CMGyroData to value, and takes care that it never goes below zero. Any thoughts why I got this crash?
var value: Double = 90
func test(d: Double)
{
value -= d
if value < 0
{
value = 0
}
else if value > 180
{
value = 180
}
// Double value cannot be converted to UInt8 because the result would be less than UInt8.min
// according to Xcode, value is 0
let angle = UInt8(value.rounded())
print(angle)
}
During the crash, the debugger shows that value is 0, and the raw memory is 0x0000000000000000. I've heard about negative zero, but then the raw memory would be 0x8000000000000000. Either way, it makes no sense for the UInt8 constructor to fail.
(lldb) p value
(Double) 0
(lldb) p value.rounded()
(Double) 0
(lldb) p UInt8(value.rounded())
(UInt8) 0
Xcode 15.1, iPhone 7 Plus, iOS 15.8
Edit: As a good measure, I changed that line to
let angle = UInt8(Int(value.rounded()))