self.motionManager
is nil and you try to unwrap a nil
value.Always unwrap optional values by checking for nil
with optional binding or use optional chaining.
if let motionManager = self.motionManager {
if let accelerometerData = motionManager.accelerometerData {
let acceleration :CMAcceleration = accelerometerData.acceleration
}
}
else {
print("motion manager is nil")
}
You should check your code if you have intialized motionManager
or not.
EDIT
I have checked documentation
Returns the latest sample of accelerometer data, or nil if none is
available.
*/
var accelerometerData: CMAccelerometerData! { get }
So you need to also check nil for accelerometerData
.It can be nil
and it is Implicitly wrapped optional so it will crash when data not available.