@backDeployed - ShenYj/ShenYj.github.io GitHub Wiki
苹果一贯风格: 新增功能应用于新版本,低版本无法享用
在调用接口时,往往会需要通过 available attribute 进行区分,以及 Swift 5.6 中新增的 unavailable attribute
@available(toasterOS 1.0, *)
public struct BreadSlice { ... }if #available(iOS 15, *) { } else {
// Code to make iOS 14 and earlier work correctly
}
if #unavailable(iOS 15) {
// Code to make iOS 14 and earlier work correctly
}在 Swift 5.8 中迎来的新的方案, @backDeployed, 它允许框架或库作者为较旧的操作系统版本提供回退实现,并允许 API 使用者在没有任何条件判断的情况下使用它。
-
示例代码:
extension Toaster { @available(iOS 15.0, *) @backDeployed(before: iOS 15.0) public func test() -> () { ... } }
这样原本在 iOS 15.0 才允许使用的 API,通过低版本下的实现处理,实现低版本的兼容