Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

found nil when reading accelerometer data

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

like image 892
MeV Avatar asked Sep 17 '26 14:09

MeV


1 Answers

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
}
like image 133
sbooth Avatar answered Sep 19 '26 04:09

sbooth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!