HKQuantity将加倍 - 从Healthkit获得活跃的能源价值

问题描述 投票:1回答:1

我正在研究一种从健康套件中读取活动能量(kcal)的方法,但是从HKQuantity获取双重值时我遇到了问题。我的代码看起来像这样:

func getActiveEnergy () {
    let endDate = NSDate()
    let startDate = NSCalendar.currentCalendar().dateByAddingUnit(.Month, value: -1, toDate: endDate, options: [])

    let energySampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierActiveEnergyBurned)
    let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)

    print ("start date: ", startDate)
    print ("end date: ", endDate)

    let query = HKSampleQuery(sampleType: energySampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
        (query, results, error) in
        if results == nil {
            print("There was an error running the query: \(error)")
        }

        dispatch_async(dispatch_get_main_queue()) {

            for activity in results as! [HKQuantitySample]
            {
                self.todayActiveEnergy = activity.quantity.doubleValueForUnit(HKUnit.countUnit())
                print(">>>>>", self.todayActiveEnergy)
            }

        }
    })
    self.healthKitStore.executeQuery(query)
}

我的问题是这一行:

 self.todayActiveEnergy = activity.quantity.doubleValueForUnit(HKUnit.countUnit())

activity.quantity确实返回了正确的值(156千卡)但是当我尝试从它获得双值时(如上所述)我得到libc++abi.dylib: terminating with uncaught exception of type NSException

知道为什么会这样吗?

swift health-kit
1个回答
4
投票

感谢OOPer的评论。将行更改为:

self.todayActiveEnergy = activity.quantity.doubleValueForUnit(HKUnit.kilocalorieUnit())

做了伎俩。

© www.soinside.com 2019 - 2024. All rights reserved.