HealthKit 中的血糖值为零

nil value of Blood Glucose from HealthKit

我在我的设备健康工具包应用程序中插入了一些葡萄糖数据,现在尝试检索这些插入的数据,但出现此错误:

2015-05-17 11:33:08.056 HKTutorial[687:125911] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to convert incompatible units: mg/dL, mol<180.1558800000541>'

这是我用来检索葡萄糖数据的方法代码:

func updateGlucose(){    
let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierBloodGlucose)


self.healthManager?.readMostRecentSample(sampleType, completion: { (mostRecentGluco, error) -> Void in

  if( error != nil )
  {
    println("Error reading blood sugar from HealthKit Store: \(error.localizedDescription)")
    return;
  }

  var glucoLocalizedString = self.kUnknownString;
  // 3. Format the weight to display it on the screen
  self.gluco = mostRecentGluco as? HKQuantitySample;

  if let mmol = self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose)) {
    let glucoFormatter = NSMassFormatter()
    glucoFormatter.forPersonMassUse = true;
    glucoLocalizedString = glucoFormatter.stringForObjectValue(mmol)!
  } else {
    println("error reading glucose data")
  }

  // 4. Update UI in the main thread
  dispatch_async(dispatch_get_main_queue(), { () -> Void in
    self.glucoLabel.text = glucoLocalizedString
    //self.updateBMI()

  });
});
}

错误恰好在这一行中断了代码执行:

if let mmol = self.gluco?.quantity.doubleValueForUnit(HKUnit.moleUnitWithMolarMass(HKUnitMolarMassBloodGlucose)) {

有什么想法吗?

问题是 葡萄糖是以浓度来衡量的,但您将其转换为体积 x。 使用的两个单位是mmol/Lmg/dL。您正在将 mg/dL 转换为 mol<180.1558800000541>,但您应该将其转换为 mmol/L