我想从我的HKHealthStore实例中获得用户最近的BMI读数。到目前为止,我正在按如下方式进行操作,但似乎不对。有没有办法得到BMI的实际数值,而不是计数单位(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];
示例返回一个数组,因此您可以简单地从数组中取出第一个对象,然后将其值转换为double进行处理。
HKQuantitySample *sample = [results firstObject];
int i = [sample.quantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"%i",i);