20.7 · What the compiler and the runtime say about each platform

The conditions, availability checks and device probes behave in ways that surprise people: AppKit imports on Catalyst, a star that is true, a watch without UIDevice.

Mental model. Ask the compiler about frameworks and the runtime about versions, and never assume the answer from the platform name.

Conditions

What each compile-time test is true for, per platform, measured.

  1. What are os(), canImport() and targetEnvironment() actually true for on each Apple platform, and where does canImport(AppKit) mislead? 20.7.1 · L3
    The first pass (Q414) gives the rule of thumb; this question is the measured table. Compiling one program for each platform and running it in the simulators…

Availability and probes

The star in availability checks, and identifying the device at run time.

  1. What does the * in #available(iOS 17, *) mean on tvOS, watchOS and visionOS, and how do you write a check that is correct on every platform? 20.7.2 · L3
    An availability condition lists platform and version pairs and ends with *, which stands for “any platform not listed, at the deployment target”. The…
  2. Which run-time probes identify the device (UIUserInterfaceIdiom, UIDevice, size classes), and what do they report on tvOS, Catalyst and the watch? 20.7.3 · L3
    Prefer capability and layout probes to device identity, but when you need identity the facts are these. UIUserInterfaceIdiom raw values are unspecified -1…

More in Multiplatform Apple