我想让用户从我的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];发布于 2014-12-22 01:41:33
该示例返回一个数组,因此您只需从数组中提取第一个对象,然后将其值转换为double进行处理。
HKQuantitySample *sample = [results firstObject];
int i = [sample.quantity doubleValueForUnit:[HKUnit countUnit]];
NSLog(@"%i",i);https://stackoverflow.com/questions/26348128
复制相似问题