我能够获取事件的基本数据,例如 duration
、totalEnergyBurned
等。
但是我如何访问 HKWasUserEntered
startDate
之类的元数据?
这是我目前的代码
var predicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Cycling)
let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let queryError = error {
print( "There was an error while reading the samples: \(queryError.localizedDescription)")
}
else
{
print(results!)
for samples: HKSample in results! {
var workout: HKWorkout = (samples as! HKWorkout)
print("workout Distance is \(workout.totalDistance)")
print("workout Energy Burned is \(workout.totalEnergyBurned)")
print("workout Duration in seconds is \(workout.duration)")
}
}
completion(results,error)
}
executeQuery(sampleQuery)
当我打印results
时,我得到了这个结果
[ (46) "Health" (9.3.2) metadata: { HKWasUserEntered = 1; } 2016-07-18 10:58:00 +0500 2016-07-18 11:58:00 +0500]
那么基本上我如何从中获取 HKWasUserEntered
和日期?
最佳答案
HKSample
上有一个metadata
字典属性。您可以像这样打印值,例如:
print("workout was user entered\(workout.metadata[HKWasUserEntered])")
关于ios - 如何使用 Swift 访问 healthkit 中锻炼的元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38452229/