在 Apple 的文档中有“子类化注释”,有时可能会说不子类化特定类。例如,HKHealthStore在其文档中有这样的措辞:“像 HealthKit 中的许多类一样,HKHealthStore 类不应该被子类化。”。
但是,在编译的教程中,我们创建了 HKHealthStore 类的一个实例并将其用于引用 HealthStore 函数。例如:
let currentHealthStore = HKHealthStore()
if HKHealthStore.isHealthDataAvailable(){
//The following is for if HealthKit is supported in this device
print("Yes, this iPhone 6 Plus supports Health Information")
let typesToRead = dataToRead()
let typesToWrite = dataToWrite()
currentHealthStore.requestAuthorization(toShare: typesToWrite as? Set<HKSampleType>, read: typesToRead as? Set<HKObjectType>, completion: { (success, error) -> Void in
if success{
// We will update UI to preview data we read.
DispatchQueue.main.async(execute: { () -> Void in
self.loadView()
})
}
else{
print("User didn't allow HealthKit to access these read/write data types")
}
})
} else {
let alertController = UIAlertController(title: "Warning", message: "HealthKit is not available in your device!", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}