【发布时间】: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