2010-07-24 59 views
1

我有一個包含七個目標的Xcode項目,對應七個iPhone應用程序。這個數字可能會增加。許多目標使用很多相同的類。Xcode多個目標 - #ifdef正在運行

我已經複製下面的應用程序委託的部分。爲了這篇文章的目的,我已經重新命名了target1到target7。我已經設置了相應的宏mTarget1到mTarget7。另外我還有一些宏,比如mTarget12,它是針對目標1和目標2定義的。

應用程序委託中的ifdef正在快速積累。爲了說明問題,我已經在下面展示了部分應用程序委託。

在光明的一面,積累看起來似乎是疊加的 - 它們並不相乘,至少現在還沒有。另一方面,應用程序委託以外的文件中的#ifdef並不是很糟糕。

在不那麼明亮的一面,應用程序代表有很多積累。

我想知道是否有更好的方法來組織這個。我應該補充一點,我不認爲單獨的Xcode項目是一種選擇 - 它會比現在更糟糕。

#ifndef mTarget34 
// an import 
#endif 

#ifdef mTarget56 
// an import 
#endif 

#ifdef mTarget7 
// an import 
#endif 

#ifdef mTarget12 
// a bunch of imports 
#endif 

#ifdef mTarget2 
// an import 
#endif 

#ifdef mTarget4 
// an import 
#endif 

@implementation xxxAppDelegate 

@synthesize window; 

#ifdef mTarget1 
// synthesize a member 
#endif 

#ifdef mTarget34 
// synthesize a member 
#endif 

#ifdef mTarget5 
// synthesize four members 
#endif 

- (void)dealloc { 
    [window release]; 
    [super dealloc]; 

#ifdef mTarget1 
    // release a member 
#endif 

#ifdef mTarget34 
    // release a member 
#endif 
} 

- (void) logMacroes { 
// a bunch more ifdef's here because it is helpful to NSLog my macroes when the program starts. 
} 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    [self logMacroes]; 
#ifdef mTarget1 
    //start analytics 
#endif 

#ifndef mTarget7 
    //start up the sound 
#endif 
#ifndef mTarget34 
# ifndef mTarget7 
// load the data model 
# endif 
#endif 
#ifdef mTarget12 
    // create a bunch of tabs for the tab bar 
#endif 

#ifdef mTarget2 
    // start analytics 
    // create a view controller that will eventually go into the tab bar. 
#endif 

#ifdef mTarget12 
NSMutableArray *vc = [[NSMutableArray alloc]initWithObjects: 
    // a bunch of objects 
# ifdef mTarget2 
// another object . . . we are still inside the initWithObjects statement here. 
# endif 
# ifdef mTarget1 
    // another object 
# endif 
         nil]; // initWithObjects finally done. 

    //release a bunch of stuff 
# ifdef mTarget2 
//another release 
# endif 
    // use the array we just created to create a tab bar controller. 
    // Add the tab bar controller to the window 
#endif 

#ifdef mTarget34 
// hide the status bar 
// create a navigation controller and add it to the window. 
#endif 

#ifdef mTarget56 
//Hide the status bar. Create a view controller and add it to the window. 
#endif 

#ifdef mTarget7 
    // create a view controller and add it to the window. 
#endif 
    [window makeKeyAndVisible]; 
} 

#ifndef mTarget34 
# ifndef mTarget7 
- (void)applicationDidEnterBackground:(UIApplication *)application { 
    // save the model 
} 

- (void) applicationWillTerminate: (UIApplication *) application { 
    // save the model 
} 
# endif 
#endif 

回答

1

在某些情況下,它可能是有意義的實際創建不同版本的實施.m文件中,每個目標甚至一個。您實際上可以將後綴追加到基本名稱,例如myDelegate_mTarget34.m等等,或者將每個文件的名稱放在目標名稱後面的子文件夾中。然後將所有實現文件添加到項目中,並將其分配給一個目標(右鍵單擊>信息>目標)