views:

139

answers:

0

I have an Xcode project with seven targets, corresponding to seven iPhone apps. That number may increase. A lot of the targets use a lot of the same classes.

I have reproduced portions of the app delegate below. For purposes of this post, I have renamed the targets target1 through target7. I have setup corresponding macroes mTarget1 through mTarget7. In addition I have macroes such as mTarget12, which is defined for targets 1 and 2.

The ifdef's in the app delegate are accumulating rapidly. In order to illustrate the problem, I've shown portions of the app delegate below.

On the bright side, the accumulation does seem to be additive -- they are not multiplying, at least not yet. Also on the bright side, the #ifdef's in files other than the app delegate are nowhere near as bad.

On the not-so-bright side, there is plenty of accumulation in the app delegate.

I'm wondering if there is a better way to organize this. I should add that I don't think separate Xcode projects is an option -- it would be worse than what I have now.

#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