尝试从Watch

时间:2017-07-29 02:55:36

标签: ios swift

我正在尝试构建一个从HealthKit读取心率数据的应用程序并将其显示在表中,但每当我运行代码时,我都会终止它,因为它在以下方法中找到nil:

扩展InterfaceController:HeartRateDelegate {

func heartRateUpdated(heartRateSamples: [HKSample]) {

    guard let heartRateSamples = heartRateSamples as? [HKQuantitySample] else {
        return
    }

    DispatchQueue.main.async {
        self.heartRateSamples = heartRateSamples
        guard let sample = heartRateSamples.first else {
            return
        }
        let value = sample.quantity.doubleValue(for: HKUnit(from: "count/min"))
        let heartRateString = String(format: "%.00f", value)
        self.heartRateLabel.setText(heartRateString)//where it finds the nil
    }
}

有没有办法将该行包装在一个可选项中?它可能不言而喻,但我对iOS编程很陌生

编辑:heartRateLabel是我在故事板中连接的插座(这都在手表界面控制器中),但我不认为我已正确初始化它。在我编写的接口控制器类中:

class InterfaceController:WKInterfaceController {

@IBOutlet var heartRateLabel: WKInterfaceLabel!

@IBOutlet var workoutButton: WKInterfaceButton!

let healthKitManager = HealthKitManager.sharedInstance 

var isWorkoutInProgress = false

var workoutSession: HKWorkoutSession?

var workoutStartDate: Date?

var heartRateQuery: HKQuery?

var heartRateSamples: [HKQuantitySample] = [HKQuantitySample]()

我需要在此步骤中初始化它吗?语法是什么?

0 个答案:

没有答案