14

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.

DrMickeyLauer
  • 4,455
  • 3
  • 31
  • 67
Bill
  • 44,502
  • 24
  • 122
  • 213
  • 1
    Did you see https://developer.apple.com/documentation/xcode/creating_a_mac_version_of_your_ipad_app - it shows what you can do. – rmaddy Oct 08 '19 at 18:35
  • I did read that, but I don't see anything about conditional compilation there. – Bill Oct 08 '19 at 18:37

2 Answers2

30

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
3

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
skymook
  • 3,401
  • 35
  • 39