我正在尝试显示用户每天的步数。但我真的不知道如何管理这个。
我已经得到了这个代码:
let endDate = NSDate()
let startDate = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitMonth, value: -1, toDate: endDate, options: nil)
let weightSampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount)
let predicate = HKQuery.predicateForSamplesWithStartDate(startDate, endDate: endDate, options: .None)
let query = HKSampleQuery(sampleType: weightSampleType, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
(query, results, error) in
if results == nil {
println("There was an error running the query: \(error)")
}
dispatch_async(dispatch_get_main_queue()) {
var dailyAVG = Int()
var steps = results as [HKQuantitySample]
for var i = 0; i < results.count; i++
{
//results[i] add values to dailyAVG
}
}
})
self.healthKitStore.executeQuery(query)
据我所知,该查询获取所需的所有数据。但我不知道如何从HKQuantitySample
. 所以我无法测试HKQuantitySample
数组中是否有正确的值。