错误:“无法调用初始化程序”,仅在Watch OS上执行该功能

时间:2022-11-20 22:48:41

I declared a failable initializer in my 'HealthKitService' which is, as the name says, a utility class for HealthKit. The problem is that certain HealthKit classes such as HKWorkoutSession, are available for Watch OS only, so I had to use a preprocessor directive to make a function available for Watch OS only:

我在我的'HealthKitService'中声明了一个可用的初始化程序,正如名称所示,它是HealthKit的实用程序类。问题是某些HealthKit类(如HKWorkoutSession)仅适用于Watch OS,因此我必须使用预处理程序指令才能为Watch OS提供一个函数:

#if TARGET_OS_WATCH
init?(activity:String) {
    // Here I initialize my HKWorkoutSession object
}
#endif

But if I call this initializer from the WatchKit extension app, this way:

但是,如果我从WatchKit扩展应用程序调用此初始化程序,这样:

if let service = HealthKitService(activity: activity) {
    // Do something with it
}

I receive this error:

我收到此错误:

Cannot invoke initializer for type 'HealthKitService' with an argument list of type '(activity: String)'

无法使用类型为“(activity:String)”的参数列表调用类型为“HealthKitService”的初始值设定项

PS: The deployment target is Watch OS 3.2, Swift version: 3.

PS:部署目标是Watch OS 3.2,Swift版本:3。

1 个解决方案

#1


1  

Try #if os(watchOS) instead of #if TARGET_OS_WATCH.

尝试#if os(watchOS)而不是#if TARGET_OS_WATCH。

#1


1  

Try #if os(watchOS) instead of #if TARGET_OS_WATCH.

尝试#if os(watchOS)而不是#if TARGET_OS_WATCH。