【问题标题】:Swift - How return Value from HealthKit classSwift - 如何从 HealthKit 类返回值
【发布时间】:2021-03-28 19:57:47
【问题描述】:

我是一个完整的初学者,只是在玩 HealthKit。也许你可以为我回答以下问题:

我有以下两个类:

//UIViewController

class ViewController: UIViewController {

@IBOutlet weak var BloodPressure: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    
    HealthControlAccess()
    
    let healthValue = HealthValue()
    healthValue.bloodPressureDiastolic()
    print (healthValue.averageDiastolic)

   }
}

//健康价值

class HealthValue {

var averageDiastolic = 0

func bloodPressureDiastolic() {
    
    print ("bloodPressureDiastolic")
    
    let store = HKHealthStore()
    
    guard let bloodPressureDiastolic = HKObjectType.quantityType(forIdentifier: .bloodPressureDiastolic) else {
        // This should never fail when using a defined constant.
        fatalError("*** Unable to get the step count type ***")
    }
    
    let query = HKSampleQuery(sampleType: bloodPressureDiastolic, predicate: nil,limit: Int(HKObjectQueryNoLimit), sortDescriptors: nil) {
        
        query, results, error in
        
        guard let samples = results as? [HKQuantitySample] else {
            // Handle any errors here.
            return
        }
        
        var pressureDiastolic : String
        var pressureDiastolicArray : [Int] = Array()

        for sample in samples {

            pressureDiastolic = "\(sample.quantity)"
            pressureDiastolicArray.append(Int(pressureDiastolic.split(separator: " ")[0])!)
        }

        if pressureDiastolicArray.count > 0 {

            self.averageDiastolic = pressureDiastolicArray.sum()/pressureDiastolicArray.count

        }

    }

    store.execute(query)
    
}

我通过 ViewController 初始化 HealthValue 类,但我没有变量 healthValue.averageDiastolic 输出的值。

我做错了什么?

【问题讨论】:

    标签: swift uiviewcontroller healthkit


    【解决方案1】:

    HKSampleQuery 是异步的。您需要等待它完成才能访问结果。通常这是通过完成处理程序完成的:

    func bloodPressureDiastolic(completion: (Double) -> Void) {
    
        ... Current code ...
    
        let query = HKSampleQuery(sampleType: bloodPressureDiastolic, predicate: nil,limit: Int(HKObjectQueryNoLimit), sortDescriptors: nil) {
    
            ... Current code ...
    
            if pressureDiastolicArray.count > 0 {
                self.averageDiastolic = pressureDiastolicArray.sum()/pressureDiastolicArray.count
            }
    
            Dispatch.main.async { [averageDiastolic] in
                completion(averageDiastolic) // <===
            }
        }
    
        store.execute(query)
    }
    

    然后你会这样称呼它:

    class ViewController: UIViewController {
    
        @IBOutlet weak var BloodPressure: UILabel!
    
        let healthValue = HealthValue()
    
        override func viewDidLoad() {
            super.viewDidLoad()
        
            HealthControlAccess() // Not sure what this is doing. May need to wait for it to complete
        
            healthValue.bloodPressureDiastolic {
                print($0)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      相关资源
      最近更新 更多