所以我正在关注苹果文档的here
我要做的是访问HKSampleQuery上的HKMetadataKeyHeartRateMotionContext密钥。如果HrContext =1并且hr大于某个数字,我需要做一些事情。
代码如下:
func sampleQ() -> HKQuery{
let pre = HKQuery.predicateForSamples(withStart: Date().lastweek, end: nil)
let que = HKSampleQuery(sampleType: heartRateType, predicate: pre, limit: Int(HKObjectQueryNoLimit), sortDescriptors: nil) {
query, results, error in
guard let samples = results as? [HKQuantitySample] else {
fatalError("An error occured fetching the user's tracked food. In your app, try to handle this error gracefully. The error was: \(String(describing: error?.localizedDescription))");
}
print (samples)
for sample in samples {
print (sample)
guard let hrContext =
sample.metadata?[HKMetadataKeyHeartRateMotionContext] as? String else {
break
}
print(hrContext)
print (hrContext+"i am the context")
}
}
return que
}然后在这里执行代码:
func startToMeasure() {
self.healthStore.execute(self.createStreamingQuery())
self.healthStore.execute(self.sampleQ())
}由于某种原因,我在日志中得到的只是正常的流心跳数据和打印的元数据(尽管我无法访问它,而且它也没有打印我的任何hrContext语句)。
有什么建议吗?
发布于 2018-02-28 10:58:31
心率-运动上下文本身的值是一个NSInteger,因此不能转换为String -该整数可以映射到HKHeartRateMotionContext的枚举情况。
let context = sample.metadata?[HKMetadataKeyHeartRateMotionContext] as? NSNumberhttps://stackoverflow.com/questions/49021119
复制相似问题