ios - Swift optional type issue -
so i'm new @ swift , ios programming. i'm trying record accelerometer , gyroscope date , display on screen. tutorials managed finish code , kind of understand code. keep getting errors. , if follow suggestion xcode gives me more errors..
so code:
import uikit import coremotion class viewcontroller: uiviewcontroller { //instance variables var currentmaxaccelx: double = 0.0 var currentmaxaccely: double = 0.0 var currentmaxaccelz: double = 0.0 var currentmaxrotx: double = 0.0 var currentmaxroty: double = 0.0 var currentmaxrotz: double = 0.0 var motionmanager = cmmotionmanager() //outlets @iboutlet var accx: uilabel? @iboutlet var accy: uilabel? @iboutlet var accz: uilabel? @iboutlet var maxaccx: uilabel? @iboutlet var maxaccy: uilabel? @iboutlet var maxaccz: uilabel? @iboutlet var rotx: uilabel? @iboutlet var roty: uilabel? @iboutlet var rotz: uilabel? @iboutlet var maxrotx: uilabel? @iboutlet var maxroty: uilabel? @iboutlet var maxrotz: uilabel? //functions @ibaction func resetmaxvalues() { currentmaxaccelx = 0 currentmaxaccely = 0 currentmaxaccelz = 0 currentmaxrotx = 0 currentmaxroty = 0 currentmaxrotz = 0 } override func viewdidload() { self.resetmaxvalues() motionmanager.accelerometerupdateinterval = 0.2 motionmanager.gyroupdateinterval = 0.2 //start recording data motionmanager.startaccelerometerupdatestoqueue(nsoperationqueue.currentqueue(), withhandler: {(accelerometerdata: cmaccelerometerdata!, error: nserror!) -> void in self.outputaccelerometerdata(accelerometerdata.acceleration) if (error != nil) { print("\(error)") } }) motionmanager.startgyroupdatestoqueue((nsoperationqueue.currentqueue()), withhandler: {(gyrodata: cmgyrodata!, error: nserror!) -> void in self.outputrotationdata(gyrodata.rotationrate) if (error! = nil) { print("\(error)") } }) super.viewdidload() } func outputaccelerometerdata(acceleration: cmacceleration) { accx?.text = "\(acceleration.x).2fg" if fabs(acceleration.x) > fabs(currentmaxaccelx) { currentmaxaccelx = acceleration.x } accy?.text = "\(acceleration.y).2fg" if fabs(acceleration.y) > fabs(currentmaxaccely) { currentmaxaccely = acceleration.y } accz?.text = "\(acceleration.z).2fg" if fabs(acceleration.z) > fabs(currentmaxaccelz) { currentmaxaccelz = acceleration.z } maxaccx?.text = "\(currentmaxaccelx) .2f" maxaccy?.text = "\(currentmaxaccely) .2f" maxaccz?.text = "\(currentmaxaccelz) .2f" } func outputrotationdata(rotation: cmrotationrate) { rotx?.text = "\(rotation.x).2fr/s" if fabs(rotation.x) > fabs(currentmaxrotx) { currentmaxrotx = rotation.x } roty?.text = "\(rotation.y).2fr/s" if fabs(rotation.y) > fabs(currentmaxroty) { currentmaxroty = rotation.y } rotz?.text = "\(rotation.z).2fr/s" if fabs(rotation.z) > fabs(currentmaxrotz) { currentmaxrotz = rotation.z } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
and error keep getting want start recording data; value of optional type 'nsoperationqueue?' not unwrapped; did mean use "!" or "?"?
sorry if stupid question want understand stuff!
thanks in advance!!
since nsoperationqueue
optional, need force unwrap using !
.
so, replace nsoperationqueue.currentqueue()
nsoperationqueue.currentqueue()!
.
you can go through apple documentation , blog understanding swift optionals.
Comments
Post a Comment