更新部分内容到 Swift 5.7 (#1200)

* 更新内容到 Swift 5.7

* 更新内容到 Swift 5.7

* 更新内容到 Swift 5.7

* update to Swift version 5.7

* 更新内容到 Swift 5.7

* 更新内容到 Swift 5.7

* 修正部分术语

* 更新内容到 Swift 5.7

* 更新内容到 Swift 5.7

* 标题格式修改

* 修改了部分用词

* 修改了代码块格式

* 修改了代码段及行内代码格式

* 修改了代码段及行内代码样式

* 按照排版要求重新修改了部分格式

* Delete 02_Lexical_Structure.md

* Delete 03_Types.md

* Delete 04_Expressions.md

* Delete 05_Statements.md

* Delete 07_Attributes.md

* Delete 10_Summary_of_the_Grammar.md

* 根据排版指南修改了部分格式

* 根据排版指南修改了部分格式

* Update source/03_language_reference/02_Lexical_Structure.md

Co-authored-by: Jie Liang <lj925184928@gmail.com>
This commit is contained in:
Robortsystem
2022-09-10 21:00:58 +08:00
committed by GitHub
parent df35244821
commit 77b5caff91
20 changed files with 1092 additions and 723 deletions

View File

@ -765,3 +765,35 @@ if #available(平台名称 版本号, ..., *) {
APIs 不可用,使用先前版本API的语句将执行
}
```
当你在 `guard` 语句中使用可用性条件时,它将细化用于该代码块中其余代码的可用性信息。
```swift
@avaliable(macOS 10.12, *)
struct ColorPreference {
var bestColor = "blue"
}
func chooseBestColor() -> String {
guard #avaliable(macOS 10.12, *) else{
return "gray"
}
let colors = ColorPreference()
return colors.bestColor
}
```
在上面的例子中,结构体 `ColorPreference` 需要 macOS 10.12 或更高的版本。函数 `ChooseBestColor()` 先以一个可用性防护开头,若平台版本过低无法运行 `ColorPreference` 时,将执行该低版本平台可用的行为。而在 `guard` 语句后,你将能够使用 macOS 10.12 或更高版本的API。
除了 `#available` 以外, Swift 还支持通过不可用性条件来进行不可用性检查。举例如下,两种检查都能实现同样的效果:
```swift
if #available(iOS 10, *){
} else {
//回滚代码
}
if #unavailable(iOS 10) {
//回滚代码
}
```
若可用性检查只提供了回滚代码,改用用 `#unavailable` 能提升程序整体的可读性。