How do I retrieve accelerometer data correctly with Swift in iOS?

user4390998 picture user4390998 · Jan 4, 2015 · Viewed 21.5k times · Source

I am trying to record the data from the iPhone's accelerometer (my own iPhone 5s) and set a label on the screen to that data using String(format: "%.2f", data) where data is the value for the specific axis I want to record. To do this I set up a CMMotionManager and began recording accelerometer data, and I have a timer that constantly updates the text in the label. However, I am getting an error from Xcode: "fatal error: unexpectedly found nil while unwrapping an Optional value". Here is the relevant code:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    //if accelerometer can be used, start it
    if (motionManager.accelerometerAvailable) {

        motionManager.accelerometerUpdateInterval = 0.1

        motionManager.startAccelerometerUpdates()
        let timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
    }



}

func update () {
    if (motionManager.accelerometerActive) {

        accelX.text = String(format: "%.2f", motionManager.accelerometerData.acceleration.x)
    }

}

The error stops when I change the accelX.text assignment to a simple string, so I think the optional variable creating the error is something to do with the accelerometer. That's as far as I know, however, so if you have any suggestions, or if I'm doing it completely wrong and there's a better and easier way, I will definitely appreciate it if you help me out.

Answer

Xiaojun picture Xiaojun · Jan 7, 2015

NSHipster has a good article to talk about the core motion: http://nshipster.com/cmdevicemotion/

A better way to regularly update UI with motion data is to use the patter as shown in below:

if manager.accelerometerAvailable {
     manager.accelerometerUpdateInterval = 0.1
     manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
     [weak self] (data: CMAccelerometerData!, error: NSError!) in
          accelX.text = String(format: "%.2f", data.acceleration.x)
     }
}