1

7 つの iPhone アプリに対応する 7 つのターゲットを持つ Xcode プロジェクトがあります。その数は増えるかもしれません。多くのターゲットが同じクラスを多数使用しています。

アプリのデリゲートの一部を以下に再現しました。この投稿のために、ターゲットの名前を target1 から target7 に変更しました。対応するマクロ mTarget1 から mTarget7 をセットアップしました。さらに、ターゲット 1 と 2 用に定義された mTarget12 などのマクロがあります。

アプリ デリゲートの 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
4

1 に答える 1

1

場合によっては、実装 .m ファイルの異なるバージョンを実際に作成することが理にかなっている場合があります。おそらくターゲットごとに 1 つです。実際には、myDelegate_mTarget34.m などのようにベース名にサフィックスを追加するか、ターゲットにちなんで名付けられたサブフォルダーに同じ名前の各ファイルを配置できます。次に、すべての実装ファイルをプロジェクトに追加し、それを 1 つのターゲットに割り当てます (右クリック > 情報 > ターゲット)。

于 2010-11-26T08:17:38.243 に答える