简体   繁体   English

我如何为血糖值构建一个 mmol/L(毫摩尔每升)的 HealthKit HKUnit?

[英]How do I construct a HealthKit HKUnit for mmol/L (millimoles per liter) for Blood Glucose values?

Blood glucose values were added back in Health in iOS 8.2: https://support.apple.com/en-us/HT203113在 iOS 8.2 中的 Health 中重新添加了血糖值: https : //support.apple.com/en-us/HT203113

How do I construct a HealthKit HKUnit for mmol/L (millimoles per liter) for Blood Glucose values?我如何为血糖值构建一个 mmol/L(毫摩尔每升)的 HealthKit HKUnit?

The following both throw exceptions: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse factorization string...以下都抛出异常:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析分解字符串...

HKUnit *mmolPerL = [HKUnit unitFromString:@"mmol<molar mass>/L"];
HKUnit *mmolPerL = [HKUnit unitFromString:@"mmol/L"];

构造两个 HKUnit,然后执行单位数学运算以创建复杂单位:

HKUnit *mmolPerL = [[HKUnit moleUnitWithMetricPrefix:HKMetricPrefixMilli molarMass:HKUnitMolarMassBloodGlucose] unitDividedByUnit:[HKUnit literUnit]];

In Swift 4:在 Swift 4 中:

let bloodGlucoseMgDlUnit = HKUnit.gramUnit(with: .milli).unitDivided(by: HKUnit.literUnit(with: .deci))
let bloodGlucoseMMolLUnit = HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: HKUnit.liter())

let mgdlValue = sample.quantity.doubleValue(for: bloodGlucoseMgDlUnit)
let mmollValue = sample.quantity.doubleValue(for: bloodGlucoseMMolLUnit)

Example:例子:

81.0 mg/dL, 4.4961063718806 mmol/L
83.0 mg/dL, 4.6071213440258 mmol/L

Can confirm that the examples provided don't work (every permutation), the alternative mg/dL unit does.可以确认提供的示例不起作用(每个排列),替代mg/dL单位起作用。

To sum up, both proposed approaches work, with similar (with the approach proposed by @cbartel the constant is actually rounded, not with the other) results regarding to the structure of the resulting HKUnit:总而言之,这两种提议的方法都有效,关于结果 HKUnit 的结构具有类似的结果(@cbartel 提出的方法,常数实际上是四舍五入的,而不是另一个):

Printing description of mmolPerL->_baseUnits->_factors:
NSMapTable {
[6] mmol<180.15588> -> 1
[7] L -> -1
}

I'd use the shorter form using the provided constant:我会使用提供的常量使用较短的形式:

HKUnit *mmolPerL = [HKUnit unitFromString:[NSString stringWithFormat:@"mmol<%f>/L",HKUnitMolarMassBloodGlucose]];

The "molar mass" passed to HKUnit's unitFromString: needs to be a decimal value representing the molar mass of the given substance.传递给 HKUnit 的 unitFromString: 的“摩尔质量”需要是表示给定物质摩尔质量的十进制值。

Blood glucose has a molar mass of ~180.156 (see HKUnitMolarMassBloodGlucose in HKUnit.h for a more precise value).血糖的摩尔质量约为 180.156(有关更精确的值,请参阅HKUnitMolarMassBloodGlucose中的 HKUnitMolarMassBloodGlucose)。 To construct this using unit strings you would want to use:要使用您想要使用的单位字符串构造它:

HKUnit *mmolPerL = [HKUnit unitFromString:@"mmol<180.156>/L"];

Here's an example in Swift 4:这是 Swift 4 中的一个示例:

let mass = HKUnitMolarMassBloodGlucose
let mmolPerLiter = HKUnit.moleUnit(with: .milli, molarMass: mass).unitDivided(by: .liter())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM