CMMotionManager nil error

error: unexpectedly found nil while unwrapping an Optional value


hiyas I for the life of me can not see whats wrong with the following codex

im a noob to swift so thats probbably why 🙂


I been stuck in this for so ling and if you can spot the propbem it be a real saver for me.


it seems to work or at least show on the xcode viewer but errors on the real phone.


many many thanks

Brian


import UIKit

import CoreMotion

class ViewController: UIViewController {

let motionManager = CMMotionManager()

override func viewDidLoad() {

if motionManager.gyroAvailable {

motionManager.deviceMotionUpdateInterval = 0.2;

motionManager.gyroUpdateInterval = 0.2

motionManager.startDeviceMotionUpdates()

motionManager.startGyroUpdatesToQueue(NSOperationQueue.currentQueue()) {

[weak self] (gyroData: CMGyroData!, error: NSError!) in

if error != nil {

println("\(error)")

}

self!.outputRotationData(gyroData.rotationRate)

}

} else {

/

}

super.viewDidLoad()

}

func outputRotationData(rotation:CMRotationRate)

{

var attitude = CMAttitude()

var motion = CMDeviceMotion()

motion = motionManager.deviceMotion

attitude = motion.attitude

if (attitude.roll > 0.00){

println("more then 0.00: \(attitude.pitch)")

}else {

println("less then 0.00: \(attitude.pitch)")

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

/

}

}

CMMotionManager nil error
 
 
Q