cmh*_*cmh 5 ios swift healthkit apple-watch
我正在尝试使用以下代码从香港心电图中检索所有症状:
func getAllSymptoms(from sample: HKElectrocardiogram) {
let catIds: [HKCategoryTypeIdentifier] = [
.rapidPoundingOrFlutteringHeartbeat,
.skippedHeartbeat,
.fatigue,
.shortnessOfBreath,
.chestTightnessOrPain,
.fainting,
.dizziness,
]
for catId in catIds {
getSymptoms(from: sample, categoryType: catId) {
(cat: HKCategoryTypeIdentifier, userEntered: Bool) in
print("\(cat) entered: \(userEntered)")
}
}
}
func getSymptoms(from sample: HKElectrocardiogram,
categoryType: HKCategoryTypeIdentifier,
completion: @escaping (HKCategoryTypeIdentifier, Bool)->Void){
guard sample.symptomsStatus == .present,
let sampleType = HKSampleType.categoryType(forIdentifier: categoryType) else {
completion(categoryType, false)
return
}
let predicate = HKQuery.predicateForObjectsAssociated(electrocardiogram: sample)
let sampleQuery = HKSampleQuery(
sampleType: sampleType,
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: nil) { (query, samples, error) in
if let sample = samples?.first,
let categorySample = sample as? HKCategorySample,
let userEntered = categorySample.metadata?["HKWasUserEntered"] {
completion(categoryType, userEntered as! Int == 1)
} else {
completion(categoryType, false)
}
}
healthStore.execute(sampleQuery)
}
Run Code Online (Sandbox Code Playgroud)
有人知道吗:
谢谢