有没有办法根据 iOS 版本为我的应用程序设置未缓存和几乎(如果不是完全)预定值,而无需在运行时手动调用初始化代码?
我不是在寻找需要我在应用程序运行时不断检查操作系统或必须在运行时通过 AppDelegate 进行调用来初始化许多全局变量的解决方案。
有没有办法根据 iOS 版本为我的应用程序设置未缓存和几乎(如果不是完全)预定值,而无需在运行时手动调用初始化代码?
我不是在寻找需要我在应用程序运行时不断检查操作系统或必须在运行时通过 AppDelegate 进行调用来初始化许多全局变量的解决方案。
You can't check the system version at compile time. That information simply doesn't exist.
The best you can do, since you want to avoid re-checking multiple times throughout a run of your app, is check very early in the process lifetime and store that information. This may be an appropriate task for your application delegate. When you get application:didFinishLaunchingWithOptions:
, do the usual runtime check and make the result available to whatever other controllers need it. Or have each controller that cares do the check when it's created.
You could also create a function with the constructor
attribute, which will be run extremely early -- before main()
, in fact, although after framework classes have been loaded. This can simply initialize a global variable with the system version that you can then access wherever you like.
使用@JoshCaswell 的建议,我尝试了以下方式:
static BOOL iOS6OrGreater;
__attribute__((constructor))
static void iOS6OrGreaterInitialization(){iOS6OrGreater = [[[UIDevice currentDevice] systemVersion] compare:@"6.0" options:NSNumericSearch] != NSOrderedDescending;}
static int MY_DEFINED_ENUM_NAME(){return (iOS6OrGreater) ? IOS6_OR_GREATER_ENUM : IOS5_OR_LESSER_ENUM;}
但是我收到了'UITextAlignmentCenter' is deprecated: first deprecated in iOS 6.0
警告。