2016-11-14 77 views
1

我有幾個目標,根據他們我需要導入標題。我可以定義一些預處理值,然後使用#ifdef來檢查它做到這一點,但我想知道,如果有什麼辦法可以這樣做:編譯期間確定目標名稱

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

回答

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 
+0

它不工作 –

+0

我將編輯答案..對不起,我沒有張貼在回答 –

+0

的時間測試它謝謝爲你的答案,但與定義預處理器宏相比,它看起來不太好。經過一番研究,我停止了這個解決方案 –