From 706f040fd6facc85e9be476f19c72685afc36d6d Mon Sep 17 00:00:00 2001 From: Shiny Zhu Date: Sun, 8 Jun 2014 09:08:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9C=E4=BE=BF=E6=8D=B7Se?= =?UTF-8?q?tter=E5=A3=B0=E6=98=8E=E2=80=9D=E4=B8=80=E8=8A=82=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/10_Properties.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/source/chapter2/10_Properties.md b/source/chapter2/10_Properties.md index 6b3f00e4..d46dd56f 100644 --- a/source/chapter2/10_Properties.md +++ b/source/chapter2/10_Properties.md @@ -155,5 +155,25 @@ println("square.origin is now at (\(square.origin.x), \(square.origin.y))") Computed Properties sample -### Setter的便捷方式 +### 便捷Setter声明 +如果计算属性的setter没有定义表示新值的参数名,则可以使用默认名称`newValue`。下面是使用了便捷Setter声明的`Rect`结构体代码: + +``` +struct AlternativeRect { + var origin = Point() + var size = Size() + var center: Point { + get { + let centerX = origin.x + (size.width / 2) + let centerY = origin.y + (size.height / 2) + return Point(x: centerX, y: centerY) + } + set { + origin.x = newValue.x - (size.width / 2) + origin.y = newValue.y - (size.height / 2) + } + } +} + +``` \ No newline at end of file