Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I conditionally compile code for Catalyst?

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.

like image 426
Bill Avatar asked Oct 08 '19 18:10

Bill


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
like image 139
rmaddy Avatar answered Dec 12 '22 03:12

rmaddy


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
like image 24
skymook Avatar answered Dec 12 '22 03:12

skymook