2017-03-20 57 views
0

我正面臨着代碼錯誤。以下代碼適用於Apple Health Kit從HealthApp讀取血糖水平。Apple HealthKit血糖水平讀數正在給出轉換錯誤

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' 的,原因是: '嘗試不兼容的單位轉換:毫克/分升,摩爾< 180.1558800000541>'

在該行self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose))

回答

0

您嘗試的轉換使無效。您無法將質量/體積(mg/dL)的數量轉換爲摩爾質量(mol)。嘗試轉換mg/dL - >mmol/L(毫摩爾每升),這是血糖測量的有效和常見的轉換。

let mmol = HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose) 
let mmolL = mmol.unitDivided(by: HKUnit.liter()) 
相關問題