* Protocol -  protocol type downcasting

* Protocols - 统一用语

* Properties - Shorthand getter

* Properties - 标点及语气调整。

* Update 10_Properties.md
This commit is contained in:
Joeytat
2019-07-04 11:36:45 +08:00
committed by Jie Liang
parent 5fc4252bfc
commit ae117bdfbc
2 changed files with 24 additions and 2 deletions

View File

@ -174,6 +174,28 @@ struct AlternativeRect {
}
```
### 简化 Getter 声明 {#shorthand-getter-declaration}
如果整个 getter 是单一表达式getter 会隐式地返回这个表达式结果。下面是另一个版本的 `Rect` 结构体,用到了简化的 getter 和 setter 声明:
```swift
struct CompactRect {
var origin = Point()
var size = Size()
var center: Point {
get {
Point(x: origin.x + (size.width / 2),
y: origin.y + (size.height / 2))
}
set {
origin.x = newValue.x - (size.width / 2)
origin.y = newValue.y - (size.height / 2)
}
}
}
```
在 getter 中忽略 `return` 与在函数中忽略 `return` 的规则相同,请参考 [隐式返回的函数](./06_Functions.md/#functions-with-an-implicit-return)。
### 只读计算属性 {#readonly-computed-properties}
只有 getter 没有 setter 的计算属性叫*只读计算属性*。只读计算属性总是返回一个值,可以通过点运算符访问,但不能设置新的值。