Swift #可用性检查的最后一个参数

Last argument of Swift #availability check

Swift 编程语言指南对#availability check 中的最后一个参数有这样的说法:

if #available(iOS 9, OSX 10.10, *) {
    // Use iOS 9 APIs on iOS, and use OS X v10.10 APIs on OS X
} else {
    // Fall back to earlier iOS and OS X APIs
}

The last argument, *, is required and specifies that on any other platform, the body of the if executes on the minimum deployment target specified by your target.

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.1).” iBooks. https://itun.es/us/jEUH0.l

我想我没有正确理解这一点 - 如果我打算让代码仅在 iOS 9 中执行并且我的最小部署目标是 8,那么当 [=19= 时我的应用程序不会崩溃吗? ] 在其他平台上并且代码在最小部署目标上执行?

最后一个参数*不表示正文执行在其他 iOS 或 OS X 的 版本,例如 iOS 8.

表示正文在最小部署目标上执行 在其他 平台 上,例如 观看OS 或电视OS。目前已知的平台列在 "Declaration Attributes" 在 Swift 文档的 "Attributes" 中:

    iOS
    iOSApplicationExtension
    OSX
    OSXApplicationExtension
    watchOS
    watchOSApplicationExtension
    tvOS
    tvOSApplicationExtension

需要最后一个参数 * 来处理所有平台 明确列出,并用于未来的平台。在你的例子中,

if #available(iOS 9, OSX 10.10, *) {

} 

块执行

  • on iOS >= 9, when 运行 on iOS platform,
  • on OS X >= 10.10, when 运行 on OS X platform,
  • 在相应的最小部署目标上,而 运行 在任何其他目标上 平台(例如 watchOS)。