Ultimate Catalyst Guide

How do I write code that only runs on macOS?

released Fri, 05 Jul 2019
Swift Version 5.1

You can implement conditional code via the new targetEnvironment(UIKitForMac directive:

Compile code only on the macOS target

#if targetEnvironment(UIKitForMac)

   let toolbar = NSToolbar(identifier: \"MyToolbar\")


Compile code only on the iOS target

#if !targetEnvironment(UIKitForMac)

   import ARKit