In Apple's documentation there are "Subclassing Notes" which sometimes may say to not subclass a specific class. For example HKHealthStore has this verbiage in its documentation: "Like many classes in HealthKit, the HKHealthStore class should not be subclassed.".
However, in a tutorial that compiles, we created one instance of the HKHealthStore class and used it for reference to the HealthStore functions. For example:
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)
}