首页 > 解决方案 > Swift Health Kit 查询

问题描述

calculateBloosPresureData我的文件中有这个功能HealthStore。我收到错误消息。

Cannot convert value of type '[HKSample]?' to expected argument type 'HKQuantitySample?'

就行了 completion(statisticsCollection)。我想在内容视图中使用数据,但无法正常工作。

 func calculateBloosPresureData(completion: @escaping (HKQuantitySample?) -> Void) {
    let presureType = HKQuantityType.correlationType(forIdentifier: .bloodPressure)!
    let startDate = Calendar.current.date(byAdding: .day, value: -7, to: Date())
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options:    .strictStartDate)
    
    query =  HKSampleQuery(sampleType: presureType,
                                predicate: predicate,
                                limit: HKObjectQueryNoLimit,
                                sortDescriptors: nil) { (query, statisticsCollection, error) in
                                    completion(statisticsCollection)
                                    print(statisticsCollection!)
                                }
                                
    if let healthstore = healthStore, let query = self.query {
        healthstore.execute(query)
    }
    
}

标签: swiftuihealthkit

解决方案


Your completion signature does not meat HQSampleQuery completion results, so just change it

func calculateBloosPresureData(completion: @escaping ([HKSample]?) -> Void) {
   ...

or transform results as needed before call to external completion(...)


推荐阅读