【问题标题】:The number 0 is being returned every time in HealthKit using swift?每次使用 swift 在 HealthKit 中返回数字 0?
【发布时间】:2017-06-28 19:19:44
【问题描述】:

我已成功写入 HealthKit,但收到这些值总是返回 0。我正在尝试返回最新的体重值。

这是我读取重量的函数:

public func readWeight(result: @escaping (Double) -> Void) {
    print("Weight")
    let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.bodyMass)

    let weightQuery = HKSampleQuery(sampleType: quantityType!, predicate: nil, limit: 1, sortDescriptors: nil) {

        query, results, error in

        if (error != nil) {
            print(error!)
            result(0.0)
        }

        guard let results = results else {
            print("No results of query")
            result(0.0)
            return
        }

        if (results.count == 0) {
            print("Zero samples")
            result(0.0)
            return
        }

        guard let bodymass = results[0] as? HKQuantitySample else {
            print("Type problem with weight")
            result(0.0)
            return
        }
        result(bodymass.quantity.doubleValue(for: HKUnit.pound()))
    }

    healthKitStore.execute(weightQuery)
}

这就是我将它设置为变量的方式:

 readWeight() { weight in
     Weight = weight
 }

谢谢!

【问题讨论】:

  • @ninjaproger 那是我的……我这样做是为了让问题更清楚一些,而且问的方式也不同
  • @arodebaugh 如果您在两个问题中都有相同的问题,那么您应该更新第一个更清楚,而不是打开另一个打开。
  • @Cristik 让另一个人更清楚地对所有这些混淆感到抱歉,我真的很想继续这个项目,但这个错误让我无法这样做
  • @arodebaugh 如果您改进了另一个问题,还需要这个吗?如果没有,你可以删除它。

标签: swift swift3 healthkit hksamplequery


【解决方案1】:

如果我正确理解您的问题,您将变量bodymass 声明为数组results[0] 中的第一个元素,它必须是数组的最后一个元素。检查您的体重的第一个值是否是健康应用程序中的0,这很可能是因为正如我所说,bodymassresults 的第一个元素或您在健康应用程序中的第一个值。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多