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

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

Noob IOS Dev 无法找出错误,请帮忙 线程 1:EXC_BREAKPOINT(代码=1,子代码=0x1003591fc) 我尽了我所能,但什么也没做(我从其他代码中了解到的一切)

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")
}

谢谢。希望尽快回复。

计步器可能还没有初始化?尝试: var pedometer = CMPedometer()

也许您还没有将 labelSteps IBOutlet 连接到 storyboard/nib 中的标签。

可能是其中一个问题,也可能是两个问题