【问题标题】:Getting most recent BMI value from HKHealthStore从 HKHealthStore 获取最新的 BMI 值
【发布时间】: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];

【问题讨论】:

    标签: ios healthkit hkhealthstore hksamplequery


    【解决方案1】:

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

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

    【讨论】:

    • 欢迎来到 Stack Overflow!通过解释代码的目的,这个答案可能会得到改进。
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多