// 首发版本
// protocol MyProtocol {
// // 这里是协议定义
// }
// 后续版本重命名了 MyProtocol
struct MyRenamedProtocol {
// 这里是协议定义
}
@available(*, unavailable, renamed:"MyRenamedProtocol")
typealias MyProtocol = MyRenamedProtocol
let a = MyProtocol()
这面这段代码,当写了avilable注解之后,由于用了参数 unavailable, 这个类型就不能使用了。 当继续使用编译时,会报错如下:
b.swift:15:9: error: 'MyProtocol' has been renamed to 'MyRenamedProtocol'
10 |
11 | @available(*, unavailable, renamed:"MyRenamedProtocol")
12 | typealias MyProtocol = MyRenamedProtocol
| `- note: 'MyProtocol' has been explicitly marked unavailable here
13 |
14 |
15 | let a = MyProtocol()
| `- error: 'MyProtocol' has been renamed to 'MyRenamedProtocol'
官方文档:docs.swift.org/swift-book/…
下面举个例子
@available(iOS, introduced: 13.0, deprecated: 100000.0, message: "Use system(_:design:weight:) instead.") 的意思
iOS:表示这个声明是和 iOS 平台相关的。这是平台名称,用于指定该声明适用的操作系统平台。introduced: 13.0:意味着这个声明是从 iOS 13.0 版本开始引入的。在 iOS 13.0 之前的版本中,这个声明不存在或者不可用。deprecated: 100000.0:这里将弃用版本设置为一个非常高的数字(100000.0),这可能是一种假设未来非常遥远的版本会弃用该功能的标记方式。通常情况下,这个数字会是一个合理的未来 iOS 版本号,用于表示从这个版本开始该声明不再推荐使用。message: "Usesystem(_:design:weight:)instead.":这是一个弃用提示消息。当开发者在一个已经标记为弃用这个声明的 iOS 版本中使用该声明时,编译器或者 IDE(集成开发环境)会显示这条消息,提示开发者应该使用system(_:design:weight:)来代替这个即将弃用或者已经弃用的声明。