Xcode 多个目标 -- #ifdef 运行结束
我有一个 Xcode 项目,有七个目标,对应于七个 iPhone 应用程序。这个数字可能会增加。许多目标使用许多相同的类。
我在下面复制了应用程序委托的部分内容。出于本文的目的,我已将目标重命名为 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在某些情况下,实际创建不同版本的实现 .m 文件可能是有意义的,甚至可能每个目标一个版本。您实际上可以在基本名称后附加一个后缀,例如 myDelegate_mTarget34.m 等...或者将每个具有相同名称的文件放在以目标命名的子文件夹中。然后将所有实现文件添加到项目中,并将其分配给一个目标(右键单击>信息>目标)
In certain cases, it might make sense to actually create different version of your implementation .m file, perhaps even one per target. You can actually append a suffix to the basename, like myDelegate_mTarget34.m etc... or place each file with the same name in a sub-folder named after the target. Then add all your implementation files into your project, and assign it to one target (right-click> Info> Targets)