double
の値を返そうとしていますが、目的の値が返されません。私はさまざまなバリエーションを試していましたが、正しい値を返すことができませんでした。ここに私はどのように到達しようとしているか見ることができます:ブロック内の値を返す方法
- (double)readData
{
__block double usersWeight;
HKQuantityType *weightType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierBodyMass];
[self.healthStore aapl_mostRecentQuantitySampleOfType:weightType predicate:nil completion:^(HKQuantity *mostRecentQuantity, NSError *error) {
if (!mostRecentQuantity) {
NSLog(@"%@",error);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Not Available");
});
}
else {
// Determine the weight in the required unit.
HKUnit *weightUnit;
if([strWeightUnit isEqualToString:@"kgs"])
{
weightUnit = [HKUnit gramUnit];
usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit];
usersWeight = usersWeight/1000.0f; //kg value
}
else
{
weightUnit = [HKUnit poundUnit];
usersWeight = [mostRecentQuantity doubleValueForUnit:weightUnit];
}
}
}];
return usersWeight;
}
元のメソッドの補完ブロックを使用して、その後、希望の値でそのブロックをコールバックすることができます。 – holex