HealthKit的血糖值为零

时间:2015-05-17 08:46:23

标签: ios swift health-kit

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

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)) {

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是葡萄糖被测量为浓度,但是您将其转换为一个体积x。 使用的两个单元是mmol/Lmg/dL 。您要将mg/dL转换为mol<180.1558800000541>,而应将其转换为mmol/L