gpt4 book ai didi

objective-c - 需要从 SWIFT 到 Objective C 的转换帮助 "healthStore.requestAuthorizationToShareTypes"

转载 作者:行者123 更新时间:2023-11-30 10:12:13 24 4
gpt4 key购买 nike

不幸的是,Apple 没有将他们的新示例翻译为 Objective C。我有一个有效的 SWIFT 代码片段,但我对 Objective C 的翻译不起作用 - 授权请求不会出现在 iPhone 上的 Objective C 代码中 -

环球银行金融电信协会:

   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) {

关于objective-c - 需要从 SWIFT 到 Objective C 的转换帮助 "healthStore.requestAuthorizationToShareTypes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340443/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com