【发布时间】:2011-11-19 06:37:12
【问题描述】:
我正在开发 2 个不同的应用程序,它们共享 95% 的相同代码和视图。使用 Xcode 解决此问题的最佳方法是什么?
【问题讨论】:
我正在开发 2 个不同的应用程序,它们共享 95% 的相同代码和视图。使用 Xcode 解决此问题的最佳方法是什么?
【问题讨论】:
使用目标。这正是他们的目的。
Learn more about the concept of targets here.
通常,大多数项目都有一个 Target,对应一个产品/应用程序。如果您定义多个目标,您可以:
例如,您可以为一个目标定义预编译器宏,为另一个目标定义其他宏(假设 OTHER_C_FLAGS = -DPREMIUM 在目标“PremiumVersion”中,OTHER_C_FLAGS = -DLITE 在“LiteVersion”目标中定义 LITE 宏),然后在您的源代码中包含类似的代码:
-(IBAction)commonCodeToBothTargetsHere
{
...
}
-(void)doStuffOnlyAvailableForPremiumVersion
{
#if PREMIUM
// This code will only be compiled if the PREMIUM macro is defined
// namely only when you compile the "PremiumVersion" target
.... // do real stuff
#else
// This code will only be compiled if the PREMIUM macro is NOT defined
// namely when you compile the "LiteVersion" target
[[[[UIAlertView alloc] initWithTitle:@"Only for premium"
message:@"Sorry, this feature is reserved for premium users. Go buy the premium version on the AppStore!"
delegate:self
cancelButtonTitle:@"Doh!"
otherButtonTitles:@"Go buy it!",nil]
autorelease] show];
#endif
}
-(void)otherCommonCodeToBothTargetsHere
{
...
}
【讨论】: