[英]how to convert Double to Int in IOS swift
将 Double 转换为 Int 将按书面方式工作。 问题是由于某种原因在运行时 self.motion.magnetometerData 为零,因此 tmp 变为零。
通常最好安全地解开可选项,例如:
let tmp = self.motion.magnetometerData?.magneticField.x
if let tmpValue = tmp {
let tmpInt = Int(tmpValue)
print(tmpInt) //And whatever else
}
或者,如果您打算在结果为零时不继续,请使用守卫:
guard let tmp = self.motion.magnetometerData?.magneticField.x else {
return //Or whatever is appropriate when the value is nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.