当前最佳版本,不需要在 NSString 中处理数字搜索就是定义macros
(见原答案:检查 iPhone iOS 版本 https://stackoverflow.com/a/5337804/492624)
这些宏确实存在于 github 中,请参阅:https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h https://github.com/carlj/CJAMacros/blob/master/CJAMacros/CJAMacros.h
像这样:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
并像这样使用它们:
if (SYSTEM_VERSION_LESS_THAN(@"5.0")) {
// code here
}
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
// code here
}
以下已过时版本
获取操作系统版本:
[[UIDevice currentDevice] systemVersion]
返回字符串,可以通过以下方式将其转换为 int/float
-[NSString floatValue]
-[NSString intValue]
像这样
两个值(floatValue、intValue)都将因其类型而被剥离,5.0.1 将变为 5.0 或 5(float 或 int),为了精确比较,您必须将其分隔为 INT 数组
在这里检查接受的答案:检查 iPhone iOS 版本 https://stackoverflow.com/a/3339787/492624
NSString *ver = [[UIDevice currentDevice] systemVersion];
int ver_int = [ver intValue];
float ver_float = [ver floatValue];
并像这样比较
NSLog(@"System Version is %@",[[UIDevice currentDevice] systemVersion]);
NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];
if (ver_float < 5.0) return false;
对于 Swift 4.0 语法
下面的示例只是检查设备是否是iOS11
或更高版本。
let systemVersion = UIDevice.current.systemVersion
if systemVersion.cgFloatValue >= 11.0 {
//"for ios 11"
}
else{
//"ios below 11")
}