我想只在当前设备的iOS版本低于特定版本时才运行一段代码,如这里所述。苹果提供的代码示例如下:
if (@available(iOS 10.0, *)) {
// iOS 10.0 and above
} else {
// below 10.0
}
然而,有些情况下我们只想在当前iOS版本低于特定版本时才运行代码。我假设以下代码可以实现:
However, there are scenarios where one would like to run code only if the current iOS version is below a specific version. I assumed the following code will work:
if (!@available(iOS 10.0, *)) {
// below 10.0
}
然而,似乎这并不起作用,并且我正在从Xcode收到以下警告:
@available does not guard availability here; use if (@available) instead
这里是添加我看到的诊断工具的 LLVM 提交记录。
有两种可能的解决方法:
- 使用
if-else
变体,而不向if
块添加任何代码(不太优雅)。 - 继续使用旧的方法,例如
-[NSProcessInfo isOperatingSystemAtLeastVersion:]
。
是否有其他可以使用 @available
的预期方式我未注意到的?
@available
。因此,基本上我能想到的唯一方法是拥有一个空的if
体,但在else
块中执行操作。这似乎是我唯一可能的方式。 - cramopy