Scott Jenner
Scott Jenner

Reputation: 21

Swift Health Kit Query

i have the function calculateBloosPresureData in my file HealthStore. I am getting the error.

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

on the line completion(statisticsCollection). I want to use the data in the content view but can't get this to work.

 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)
    }
    
}

Upvotes: 1

Views: 170

Answers (1)

Asperi
Asperi

Reputation: 257503

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(...)

Upvotes: 1

Related Questions