Search code examples
iospedometer

Pedometer simple app "Fatal error: unexpectedly found nil while unwrapping an Optional value"


Noob IOS Dev can't figure out the error please, please some help thread1: EXC_BREAKPOINT (code=1,subcode=0x1003591fc) Ive tried everything I could and nothing (well everything I understood from other codes)

var pedometer :CMPedometer!
var stepCount :Int = 0
@IBOutlet var labelSteps: UILabel!
@IBAction func btnStart(_ sender: Any) {
    *Pedometer*
    if CMPedometer.isStepCountingAvailable() {
        self.pedometer.startUpdates(from: NSDate() as Date, withHandler: {
            data, error in
            if data != nil {
                // Add to existing counts
                self.stepCount += (data?.numberOfSteps as! Int)
                self.labelSteps.text = "\(self.stepCount)"

            }
        })
    }
}

@IBAction func btnStop(_ sender: Any) {
    //self.labelSteps.text = "0"
    //self.pedometer.stopUpdates()
    print("Stop")
}

Thanks. Hoping for a response soon.


Solution

  • Pedometer may not have been initialised? Try: var pedometer = CMPedometer()

    Perhaps you haven't hooked up your labelSteps IBOutlet to the label in your storyboard/nib.

    Could be one of those issues or both