Vlad Hatko Vlad Hatko - 22 days ago 6
iOS Question

Determine target name during compilation

I have several targets and depending on them I need to import headers. I can do this by defining some preprocessor value and then check it with #ifdef, but I'm wondering, if there any way to do something like:

#if TARGET_NAME = "FirstTarget"
#import "SomeHeader.h"
#endif

Answer

u can use like below for example,

//Your current target's are defined
#define FIRST_TARGET   "FirstTarget"
#define SECOND_TARGET  "SecondTarget"
#define THIRD_TARGET   "ThirdTarget"  

//set which target u want to run
#define TARGET_NAME   FIRST_TARGET //SECOND_TARGET //THIRD_TARGET

//use like below

#if (TARGET_NAME == FIRST_TARGET) //or u can directly specify name instead of defining at the beginning
   #import "SomeHeader.h"
   //other headers
#elif (TARGET_NAME == SECOND_TARGET)
   //header to be in second target
#elif (TARGET_NAME == THIRD_TARGET)
   // other header
#endif

Edit:

i got it, it gives error because macros doesn't compare variable length values, for more details see this hear .

so in order to work u can change it like below,

//Your current target's are defined, instead of strings give some constant values
#define FIRST_TARGET   1//@"FirstTarget"
#define SECOND_TARGET  2//@"SecondTarget"
#define THIRD_TARGET   3//@"ThirdTarget"

//set which target u want to run 
#define TARGET_NAME   FIRST_TARGET //SECOND_TARGET //THIRD_TARGET

//use like below
#if TARGET_NAME == FIRST_TARGET
#import "SomeHeader.h"
//other headers
#elif (TARGET_NAME == SECOND_TARGET)
//header to be in second target
#elif (TARGET_NAME == THIRD_TARGET)
// other header

#endif