作者:平凡特产小店 | 来源:互联网 | 2023-05-18 06:23
IwouldliketorunapieceofcodeonlyiftheiOSversionofthecurrentdeviceisbelowaspecifi
I would like to run a piece of code only if the iOS version of the current device is below a specific version, as specified here. The code examples given by Apple look like this:
我只希望在当前设备的iOS版本低于特定版本的情况下运行一段代码。苹果给出的代码示例如下:
if (@available(iOS 10.0, *)) {
// iOS 10.0 and above
} else {
// below 10.0
}
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:
但是,有些情况下,只有在当前的iOS版本低于特定版本时,才会运行代码。我假设下面的代码可以工作:
if (!@available(iOS 10.0, *)) {
// below 10.0
}
However it seems that this doesn't work, and I'm getting the following warning from Xcode:
然而,这似乎不起作用,我得到了Xcode的以下警告:
@available does not guard availability here; use if (@available) instead
Here is the LLVM commit that added the diagnostic I'm seeing.
这里是LLVM提交,添加了我所看到的诊断。
There are two possible fallbacks to that issue:
对于这个问题有两种可能的退步:
- Use the
if-else
variant without adding any code to the if
block (not very elegant).
- 使用if-else变体而不向if块添加任何代码(不是非常优雅)。
- Continue to use old approaches such as
-[NSProcessInfo isOperatingSystemAtLeastVersion:]
.
- 继续使用旧的方法,例如-[NSProcessInfo isoperatingsystemat至少version:]。
Is there another intended way to use @available
that I'm missing?
是否有另一种可用的方法来使用@available ?
1 个解决方案