title: How do I retrieve accelerometer data correctly with Swift in iOS?
body: <p>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 <code>String(format: "%.2f", data)</code> 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:</p>
  
  <pre><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)
      }
  
  }
  </code></pre>
  
  <p>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.</p>