有没有办法根据结构的使用平台定义协议 属性?

Is there a way to define a protocol property for a struct dependent on which platform it is being used on?

我正在使用 iOS 和 WatchOS。

我有一些正在 iPhone 上创建并发送到 Apple Watch 的结构。该结构的目标成员属于 iOS 应用程序和 WatchKit 扩展。

我想为这些只能在 WatchKit 扩展中访问的结构定义一个协议 属性:

var complicationTimelineEntry: CLKComplicationTimelineEntry? { get }

CLKComplicationTimelineEntryClockKit 框架的一部分,在 iOS 上不可用,因此我无法编译。

有办法吗?还是放弃一刀切的方法,分解问题?

目前,我正在使用另一种协议在 WatchOS 端为结构创建扩展。它有效,但比我想要的多了几个步骤。

你可以这样做:

protocol xyz {
  #if os(watchOS)
      var complicationTimelineEntry: CLKComplicationTimelineEntry? { get }
  #endif
  .
  .
  .
}

if 基本上会检查 os 应用 运行 的状态。

其他可用选项 os(iOS)os(tvOS)os(OSX)