I am having trouble using the accelerometer and I don't understand why I am not able to read the data:
I check if the accelerometer is available:
if self.motionManager.accelerometerAvailable == true {
I start the accelerometer updates:
self.motionManager.startAccelerometerUpdates()
Then i try to read the data but i get an error:
println("data.acceleration.x is: \(self.motionManager.accelerometerData.acceleration.x)")
unexpectedly found nil while unwrapping an Optional value
Why is that happening?
(If i use the startAccelerometerUpdatesToQueue it works but the updates are too slow)
Any help is appreciated, Thank you
accelerometerData can be nil if none is available. It is declared as an implicitly unwrapped optional CMAccelerometerData!, so you should check for nil before using it:
if self.motionManager.accelerometerData != nil {
// Got it
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With