Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple targets in XCode using swift language?

Tags:

xcode

I have created two targets in XCode but I am using swift. Can anybody know please how to handle multiple targets using swift? As we were doing in Objective-C as given in below example.

#if defined(TARGET_LITE)

      NSLog("Lite version");

 #else

      NSLog("Original version");

 #endif

Thanks in advance

like image 262
Milan Rathod Avatar asked Oct 31 '15 09:10

Milan Rathod


People also ask

How do I run targets in Xcode?

Go to project's settings in the Project Navigator panel. Under the Targets sections, right click the existing target and select Duplicate to copy your existing target. 2. Xcode will ask you if your new target is for iPad development.

Where is target settings in Xcode?

Choose the project in the Project Navigator on the left. Select the Configurations target from the Targets section and click the Build Settings tab at the top. The Build Settings tab shows the build settings for the Configurations target. It's possible to expand this list with build settings that you define.


2 Answers

You could of cause use preprocessor statements like #if in Swift but I think you need to define the target-variables by yourself. I am using it as follows:

Build Settings -> Swift Compiler Flags -> Other Swift Flags Set a variable -D LITE in your lite-target configuration

In your code you could use it like this:

#if LITE
   // do something
#else
  // do something else
#endif
like image 151
dibi Avatar answered Oct 20 '22 04:10

dibi


Under the target's Build Settings search for "flags" and add -D TARGET_LITE to Swift Compiler - Custom Flags > Other Swift Flags:

enter image description here

Observe how it's getting passed to the compiler by looking in the build log (hard to read, sorry):

enter image description here

like image 4
trojanfoe Avatar answered Oct 20 '22 02:10

trojanfoe