从 SWIFT 到 Objective C "healthStore.requestAuthorizationToShareTypes" 需要翻译帮助

Translation help needed from SWIFT to Objective C "healthStore.requestAuthorizationToShareTypes"

遗憾的是,Apple 没有将他们的新示例翻译成 Objective C。我有一个可用的 SWIFT 代码片段,但我对 objective C 的翻译不起作用 - 授权请求未出现在 iPhone 上的 objective c 代码中 -

SWIFT:

   class InterfaceController: WKInterfaceController {

   let healthStore = HKHealthStore()


    override func willActivate() {
       super.willActivate()

       guard let quantityType = HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else {
            return
       }

       let dataTypes = Set(arrayLiteral: quantityType)
       healthStore.requestAuthorizationToShareTypes(nil, readTypes: dataTypes) { (success, error) -> Void in
           if success == false {

           }
       }
    }

   }

Objective-C:

@interface InterfaceController()

@property HKHealthStore * healthScore;

@end


@implementation InterfaceController


- (void)willActivate {

[super willActivate];

NSString * quantity = HKQuantityTypeIdentifierHeartRate;

HKQuantityType * quantityType = [HKQuantityType quantityTypeForIdentifier:quantity];

NSSet <HKQuantityType *> * dataTypes = [NSSet setWithArray:@[quantityType]];

[self.healthScore requestAuthorizationToShareTypes:nil readTypes:dataTypes completion:^(BOOL success, NSError * _Nullable error) {
if (!success) { } }];
}

@end

您在使用它之前缺少创建 healthScore

let healthStore = HKHealthStore() 创建实例。

// Missing initialization
self.healthScore = [HKHealthStore new];

...

[self.healthScore requestAuthorizationToShareTypes:nil readTypes:dataTypes completion:^(BOOL success, NSError * _Nullable error) {