问题
I've got a team working on some IOS code. One developer (me) has Xcode6-beta with a few lines of IOS8 specific code. When another developer with Xcode5 builds it for IOS7, compilation fails, because the IOS8 SDK doesn't exist for him.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
// ios8 code
} else {
// ios7 and less way
}
Is there a way to ignore the IOS8 code in Xcode compilation based on the SDK or Xcode version, such that it compiles?
回答1:
Yes, you can do this using directives:
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
// target is iOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000
// target is lower than iOS 8.0
#else
// target is at least iOS 8.0
#endif
#endif
This is similar to commenting out unneeded lines of code but automatically. Compiller does not include everything which is stored 8.0-specific part of the if-statement.
回答2:
This link is great. But in short ask the object if a certain method respondsToSelector:
if ([self.<object_name> respondsToSelector:@selector(<method_in_question>)]) {
[self.<object_name> <method_in_question>];
}
This is much more dynamic than checking the actual OS version. Good luck!
来源:https://stackoverflow.com/questions/24268070/ignore-ios8-code-in-xcode-5-compilation