致命错误:意外发现零而展开的可选值CMMotionManager
问题描述:
试图实现在SWIFT这个raywenderlich tutiorial,但不幸的是我致命错误:意外发现零而展开的可选值CMMotionManager
fatal error: unexpectedly found nil while unwrapping an Optional value
上线
let acceleration :CMAcceleration = self.motionManager!.accelerometerData.acceleration
任何机构可以请帮助为什么其发生
请从现场文件here
答
self.motionManager
为零,您尝试打开nil
value.Always通过检查nil
使用可选绑定或使用可选链接来打开可选值。
if let motionManager = self.motionManager {
if let accelerometerData = motionManager.accelerometerData {
let acceleration :CMAcceleration = accelerometerData.acceleration
}
}
else {
print("motion manager is nil")
}
如果您已经初始化motionManager
,您应该检查您的代码。
编辑
我已签文件
Returns the latest sample of accelerometer data, or nil if none is available. */
var accelerometerData: CMAccelerometerData! { get }
所以你还需要检查零为accelerometerData
。它可以nil
,它是隐式包可选的,所以它会崩溃时无法使用数据。
我已经在检查零号,请查看上传的文件 – Ali 2014-12-27 18:36:45
@Ali编辑答案看看 – codester 2014-12-27 18:57:41