Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 11中的NSFoundationVersionNumber-如何以编程方式检测iOS版本_Ios_Objective C_Iphone - Fatal编程技术网

iOS 11中的NSFoundationVersionNumber-如何以编程方式检测iOS版本

iOS 11中的NSFoundationVersionNumber-如何以编程方式检测iOS版本,ios,objective-c,iphone,Ios,Objective C,Iphone,在以前的iOS版本中,我使用NSFoundationVersionNumber检测iOS版本: #define IS_IOS10orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) #define IS_IOS9orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_3) #

在以前的iOS版本中,我使用
NSFoundationVersionNumber
检测iOS版本:

#define IS_IOS10orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max)
#define IS_IOS9orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_8_3)
#define IS_IOS8orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1)
#define IS_IOS7orHIGHER (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
#define IS_IOS6orHIGHER (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_0)
...
现在我想对iOS 11执行同样的操作,但我无法找到正确的
NSFoundationVersionNumber
。显示
NSFoundationVersionNumber\u iOS\u 9\u x\u Max
为最新版本

那么,如何使用正确的
NSFoundationVersionNumber
来检测是否使用了iOS 11?

Apple现在在NSProcessInfo中提供NSOperatingSystemVersion结构

您可以将它与IsoperatingSystemAtleLastVersion:一起使用,或者只需检查这3个字段,就可以确切地知道哪个系统版本正在运行您的应用程序


它在obj c和swift中都可用

获取系统版本的指定方法之一是使用UIDevice的systemVersion属性。

NSString*systemVersion=[[UIDevice currentDevice]systemVersion]

您可以这样使用它:

#define IS_IOS11orHIGHER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)

如果您使用的是Xcode 9或更高版本,则可以使用
@available
属性:

if (@available(iOS 11.0, *)) {
    NSLog(@"newest");
} else {
    NSLog(@"not newest");
}
在Swift中,它是相同的,但拼写为
#available
,而不是
@available


与其他方法相比,这是一种更好的方法,因为编译器知道它,并且会适当地抑制可用性警告。因此,在上面的示例中,您可以在
if
块中使用iOS 11.0中引入的API,而不会被警告这些API对您的项目部署目标不可用。

我喜欢这种新方法:)根据我上面的评论,由于使用浮点数进行比较,接受的答案可能会有舍入误差-这始终是细微误差的来源。在我看来,这是一个更好的方法。要清楚,在我看来,@CharlesSrstka的这个答案是正确的,应该是可以接受的答案,而不是使用
系统版本的浮点比较的答案,因为这可能会导致细微的错误。@onmyway133是
@可用的
if
语句中唯一的条件,还是通过
&
|
将其与其他内容组合?后者会给你这个警告。不幸的是,可用性检查必须自己进行。@KevinAmiranoff iOS 11(或您检查的任何版本)及以上版本。如果需要专门检查某个特定的操作系统版本,最好使用
NSProcessInfo
operatingSystemVersion
属性:实际上,这可能是由于浮点运算造成的故障<代码>[[UIDevice currentDevice]systemVersion]floatValue]
在我的系统上是
11.1999998
。标准的浮点取整问题,所以如果我在寻找11.2,这将失败。