How can I conditionally compile code for Catalyst?

Viewed 3537

I'm working on porting an iOS application to Catalyst. The Catalyst (Mac) version will have its own target.

Is there an official way to conditionally compile code just for Catalyst? Otherwise, I can add a target-specific define, but it would be better to use something more general.

2 Answers

As seen in the documentation Creating a Mac Version of Your iPad App, you do:

Swift:

#if targetEnvironment(macCatalyst)
    // Code specific to Mac.
#else
    // Code to exclude from Mac.
#endif

Objective-C:

#if TARGET_OS_MACCATALYST
    // Code specific to Mac.
#else
    // Code to exclude from Mac.
#endif

It is also possible to run code that is excluded from macCatalyst without having to use the #else. Note the use of ! (not).

#if !targetEnvironment(macCatalyst)
    print("This code will not run on macCatalyst")
#endif
Related