0

We'd like to find a way to write iOS-only Swift code blocks in code that supports multiple Apple platforms and runs without warnings on both Xcode 14 and 15.

It's complicated by the fact that #if os(iOS) evaluates to true for visionOS so the test #if os(iOS) && !os(visionOS) is needed to build iOS-only code on Xcode 15. However, this generates a warning on Xcode 14 since visionOS is not recognized.

#if os(iOS) && (swift(<5.9) || !os(visionOS)) also generates warnings on Xcode 14.

We've found that the following runs warning free on both:

    #if swift(>=5.9)
      #if os(iOS) && !os(visionOS)
        code block
      #endif
    #else 
      #if os(iOS)
        code block
      #endif
    #endif

But this is ugly since the code block needs to be duplicated. Is there a better way?

Paul Beusterien
  • 27,542
  • 6
  • 83
  • 139

1 Answers1

0

Under Xcode 14.3 and Xcode 15, the following will allow you to compile without any warnings:

#if swift(>=5.9) && os(visionOS)
    // visionOS code, if any, under Xcode 15
#elseif os(iOS)
    // iOS code under Xcode 14/15
#endif

Of course you can add other #elseif os(xxx) as needed (for example, macOS).

Under Xcode 14.2 you will get a warning about the unknown visionOS but the code will otherwise build correctly without any code duplication.

HangarRash
  • 7,314
  • 5
  • 5
  • 32
  • That would be nice, but Xcode 14.2 warns with "Unknown operating system for build configuration 'os'" – Paul Beusterien Jul 08 '23 at 14:06
  • I don't get that warning with Xcode 14.3. Can you update to Xcode 14.3 from 14.2? Or are you somehow using Swift 5.9 with Xcode 14.2? – HangarRash Jul 08 '23 at 14:34
  • This is for SDK code that we want to support back to 14.1. And yes, it looks like Swift 5.8 doesn't short-circuit evaluating the operands of the `&&` operation – Paul Beusterien Jul 08 '23 at 17:48
  • But Xcode 14.3 uses Swift 5.8 and I don't have any warnings using the code posted in my answer. Curious what the benefit of supporting Xcode 14.1 versus 14.3 is. I certainly understand Xcode 14 vs 15 but not 14.1 vs 14.3. Anyone running Xcode 14.1 should have no reason to avoid upgrading Xcode 14.3 (other than the large download). – HangarRash Jul 08 '23 at 18:34
  • As an SDK vendor, we prefer to avoid being the reason developers must change their Xcode version. Xcode 14.3 is especially significant because it also requires a macOS upgrade to Ventura. – Paul Beusterien Jul 08 '23 at 18:40
  • I forgot about the need for macOS 13 for Xcode 14.3. – HangarRash Jul 08 '23 at 18:49