Error: "cannot invoke initializer" for function to be executed on Watch OS only

Error: "cannot invoke initializer" for function to be executed on Watch OS only

我在 'HealthKitService' 中声明了一个可失败的初始化器,顾名思义,它是一个用于 HealthKit 的实用程序 class。问题是某些 HealthKit classes,例如 HKWorkoutSession,只能用于 Watch OS,所以我不得不使用预处理器指令来使函数可用仅限手表 OS:

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

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

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

我收到此错误:

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

PS:部署目标是WatchOS3.2,Swift版本:3.

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