2011-09-17 58 views

回答

35

使用目標。這正是他們的目的。

Learn more about the concept of targets here

通常,大多數項目都有一個目標,它對應於一個產品/應用程序。如果定義多個目標,您可以:

  • 包括一些在這兩個目標,你的源代碼文件(或者全部),有的在一個目標和一些其他
  • 你可以構建設置發揮使用不同的設置編譯這兩個目標。

例如,你可以定義預編譯宏的一個目標和其他宏爲其他(假設OTHER_C_FLAGS = -DPREMIUM靶「PremiumVersion」和OTHER_C_FLAGS = -DLITE來定義「LiteVersion」目標LITE宏),然後在您的源代碼中包含類似代碼:

-(IBAction)commonCodeToBothTargetsHere 
{ 
    ... 
} 

-(void)doStuffOnlyAvailableForPremiumVersion 
{ 
#if PREMIUM 
    // This code will only be compiled if the PREMIUM macro is defined 
    // namely only when you compile the "PremiumVersion" target 
    .... // do real stuff 
#else 
    // This code will only be compiled if the PREMIUM macro is NOT defined 
    // namely when you compile the "LiteVersion" target 

    [[[[UIAlertView alloc] initWithTitle:@"Only for premium" 
     message:@"Sorry, this feature is reserved for premium users. Go buy the premium version on the AppStore!" 
     delegate:self 
     cancelButtonTitle:@"Doh!" 
     otherButtonTitles:@"Go buy it!",nil] 
    autorelease] show]; 
#endif 
} 

-(void)otherCommonCodeToBothTargetsHere 
{ 
    ... 
} 
+1

如果您習慣了它,請將目標視爲不同的生成文件。他們有相同的角色。 – Cyrille

+2

這很大,謝謝。爲了讓這真的適合我,我需要做一個複製目標,然後更新目標設置,以使用不同的Info.plist作爲我的軟件包名稱等... – Slee

+4

請注意,如果您進一步向項目添加文件,小心將文件添加到兩個目標中,而不僅僅是活動的文件:在添加文件時,在出現詢問是否要複製文件等的對話框中,每個目標和複選框都有一個列表。 (複選框狀態已保存,因此您可能需要僅檢查一次) – AliSoftware

相關問題