Skip to content

Instantly share code, notes, and snippets.

@ole
Created June 9, 2022 15:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/06e4582615a378bce1499bdf58e3c404 to your computer and use it in GitHub Desktop.
Save ole/06e4582615a378bce1499bdf58e3c404 to your computer and use it in GitHub Desktop.
// I learned about `#if canImport(module, _version: x.y)` from Tony Allevato:
// https://forums.swift.org/t/pitch-sdk-conditional-code/52642/4
//
// Does this work reliably? I have no idea!
// I have only done some quick experiments in a playground.
//
// The Foundation version in Xcode 14.0b1 is:
// - in iOS's Foundation.swiftinterface: 1932.104
// - in macOS's Foundation.swiftinterface: 1932.401
//
// Checking for the smaller number seems to work for both OSes.
#if canImport(Foundation, _version: 1932.104)
print("We're using the macOS 13/iOS 16 base SDK")
#elseif canImport(Foundation)
print("We're using an older base SDK")
#endif
@ole
Copy link
Author

ole commented Jun 9, 2022

If you run this code in Xcode 14.0b1, you'll enter the first branch. If you run it in Xcode 13.4, you'll enter the second branch. Tested in a macOS and iOS playground, respectively. I haven't tested this in a real project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment