从 HKHealthStore 获取最新的 BMI 值

Posted

技术标签:

【中文标题】从 HKHealthStore 获取最新的 BMI 值【英文标题】:Getting most recent BMI value from HKHealthStore 【发布时间】:2014-10-13 20:12:04 【问题描述】:

我想从我的 HKHealthStore 实例中获取用户最近的 BMI 读数。截至目前,我正在按照以下方式进行操作,但似乎不正确。有没有办法获得 BMI 的实际数值而不是 countUnit (HKUnit)?

HKQuantityType *bodyMassIndexType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMassIndex];

    HKSampleQuery *bmiSampleQuery = [[HKSampleQuery alloc] initWithSampleType:bodyMassIndexType predicate:nil limit:1 sortDescriptors:nil resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) 

        if (results.count == 0)
        
            //No results
        
        else
        
            if (!error)
            
                NSString *bmiString = [NSString stringWithFormat:@"%@", [[results firstObject] quantity]];
                NSString *parsedBMIString = [bmiString stringByReplacingOccurrencesOfString:@" count" withString:@""];
                NSLog(@"%f", [parsedBMIString floatValue]);
            
        
    ];

    [self.store executeQuery:bmiSampleQuery];

【问题讨论】:

【参考方案1】:

示例返回一个数组,因此您可以简单地从数组中取出第一个对象,然后将其值转换为双精度值进行处理。

HKQuantitySample *sample = [results firstObject];
int i = [sample.quantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"%i",i);

【讨论】:

欢迎来到 Stack Overflow!通过解释代码的目的,这个答案可能会得到改进。

以上是关于从 HKHealthStore 获取最新的 BMI 值的主要内容,如果未能解决你的问题,请参考以下文章

如何从 HealthKit 数据中获取最新的体重条目

如何从 HKHealthStore 中删除多个对象?

如何从 HKHealthStore 中删除多个对象?

Apple watch HealthKit - 无法识别的选择器 HKHealthStore startWorkoutSession:completion:

Apple watch HealthKit - 无法识别的选择器 HKHealthStore startWorkoutSession:completion:

在SAS DS2中,如何创建一个简单的程序来计算bmi