获得静息心率

时间:2018-04-28 10:01:47

标签: ios swift4 health-kit

美好的一天,

我是Swift 4的新手,在收到静息心率数据时遇到了麻烦。

这就是我所拥有的:

//声明     var heartRateType = HKAnchoredObjectQuery.self

private func createStreamingQuery() -> HKQuery {

    let calendar = NSCalendar.current;

    let now = NSDate();

    let sevenDaysAgo = calendar.date(byAdding: .day, value: -7, to: now as Date);

    let startDate = calendar.startOfDay(for: sevenDaysAgo!);

    let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: now as Date, options: [])

    let query = HKAnchoredObjectQuery(type: heartRateType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) {

        (query, samples, deletedObjects, anchor, error) -> Void in

        self.formatSamples(samples: samples)

    }

    query.updateHandler = { (query, samples, deletedObjects, anchor, error) -> Void in
        self.formatSamples(samples: samples)
    }

    return query
}

@IBAction func readHeartRate(_ sender: Any) {
    self.healthKitStore.execute(self.createStreamingQuery())
}

private func formatSamples(samples: [HKSample]?) {
    guard let heartRateSamples = samples as? [HKQuantitySample] else { return }

    guard let sample = heartRateSamples.first else{return}
    let value = sample.quantity.doubleValue(for: heartRateType)

    print("HeartRate: \(value)")
}

我在这些行中遇到以下错误,无法检查这是否有效。

//错误:通用参数' T'无法推断

//错误代码:

    let query = HKAnchoredObjectQuery(type: heartRateType, predicate: predicate, anchor: nil, limit: Int(HKObjectQueryNoLimit)) {

        (query, samples, deletedObjects, anchor, error) -> Void in

        self.formatSamples(samples: samples)

    }

另一个错误:无法转换类型' HKAnchoredObjectQuery.Type'的值预期参数类型' HKUnit'

//错误代码:

    let value = sample.quantity.doubleValue(for: heartRateType)

非常感谢您提供的任何帮助。

非常感谢你!

凯文

1 个答案:

答案 0 :(得分:0)

自从我发布此信息以来,我已经进行了研究并了解了更多信息。我最终将函数重写为以下内容:

User.IsInRole