[英]Apple HealthKit Glucose level reading is giving conversion error
I am facing below error for the code. 我面临以下代码错误。 below code is for Apple health kit reading glucose levels from HealthApp. 以下代码用于Apple Health Kit从HealthApp读取葡萄糖水平。
func updateGluco(){ func updateGluco(){
let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)
self.healthManager?.readMostRecentSample(sampleType, completion: {(mostRecentGluco, error) -> Void in
if (error != nil){
println("Error reading blood glucose from HealthKit store: \(error.localizedDescription)")
return;
}
var glucoLocalizedString = self.kUnknownString;
self.gluco = mostRecentGluco as? HKQuantitySample
println("\(self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose)))")
self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose))
if let mmol = self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose)) {
glucoLocalizedString = "\(mmol)"
} else {
println("error reading gluco data!")
}
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.glucoLabel.text = glucoLocalizedString})
})
} }
'NSInvalidArgumentException', reason: 'Attempt to convert incompatible units: mg/dL, mol<180.1558800000541>' 'NSInvalidArgumentException',原因:“试图转换不兼容的单位:mg / dL,mol <180.1558800000541>”
at this line self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose)) 在此行self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose))
The conversion you are attempting to make is invalid. 您尝试进行的转换无效。 You cannot convert a mass / volume ( mg/dL
) quantity to just a molar mass ( mol
). 您无法将质量/体积( mg/dL
)量转换为摩尔质量( mol
)。 Try converting mg/dL
-> mmol/L
(millimoles per liter), which is a valid and common conversion for blood glucose measurements. 尝试转换mg/dL
> mmol/L
(毫摩尔/升),这对于血糖测量是有效且常见的转换。
let mmol = HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose)
let mmolL = mmol.unitDivided(by: HKUnit.liter())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.