2010-07-24 35 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等等,或者将每个文件的名称放在目标名称后面的子文件夹中。然后将所有实现文件添加到项目中,并将其分配给一个目标(右键单击>信息>目标)