在编译期间确定目标名称

时间:2016-11-14 10:42:47

标签: ios xcode

我有几个目标,根据它们我需要导入标题。我可以通过定义一些预处理器值然后用#ifdef来检查它,但我想知道,如果有任何方法可以做类似的事情:

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

1 个答案:

答案 0 :(得分:0)

你可以使用如下,例如,

//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

修改

我知道了,它给出了错误,因为宏不会比较变量长度值,有关详细信息,请参阅此hear

所以为了工作你可以像下面一样改变它,

//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