@@ -57,16 +57,15 @@ class Counter {
57
57
```
58
58
-->
59
59
60
- The ` Counter ` class defines three instance methods:
60
+ ` Counter ` 类定义了三个实例方法:
61
61
62
- - ` increment() ` increments the counter by ` 1 ` .
63
- - ` increment(by: Int) ` increments the counter by a specified integer amount.
64
- - ` reset() ` resets the counter to zero.
62
+ - ` increment() ` 将计数器增加 ` 1 ` 。
63
+ - ` increment(by: Int) ` 将计数器增加指定的整数值。
64
+ - ` reset() ` 将计数器重置为零。
65
65
66
- The ` Counter ` class also declares a variable property, ` count ` ,
67
- to keep track of the current counter value.
66
+ ` Counter ` 类还声明了一个变量属性 ` count ` ,用于跟踪当前的计数器值。
68
67
69
- You call instance methods with the same dot syntax as properties:
68
+ 你可以使用与属性相同的点语法来调用实例方法:
70
69
71
70
``` swift
72
71
let counter = Counter ()
@@ -98,20 +97,15 @@ counter.reset()
98
97
```
99
98
-->
100
99
101
- Function parameters can have both a name (for use within the function's body)
102
- and an argument label (for use when calling the function),
103
- as described in < doc:Functions#Function-Argument-Labels-and-Parameter-Names > .
104
- The same is true for method parameters,
105
- because methods are just functions that are associated with a type.
100
+ 函数参数可以同时拥有一个参数名称(在函数体内使用)和一个参数标签(在调用函数时使用),参见< doc:Functions#Function-Argument-Labels-and-Parameter-Names > 。
101
+ 方法参数也是如此,因为方法只是与某种类型关联的函数。
106
102
107
- ### The self Property
103
+ ### self 属性
108
104
109
- Every instance of a type has an implicit property called ` self ` ,
110
- which is exactly equivalent to the instance itself.
111
- You use the ` self ` property to refer to the current instance
112
- within its own instance methods.
105
+ 每个类型的实例都有一个隐式属性,称为 ` self ` ,它与实例本身完全等同。
106
+ 你可以在实例方法中使用 ` self ` 属性来引用当前实例。
113
107
114
- The ` increment() ` method in the example above could have been written like this:
108
+ 上面示例中的 ` increment() ` 方法可以这样编写:
115
109
116
110
``` swift
117
111
func increment () {
@@ -136,22 +130,14 @@ func increment() {
136
130
NOTE: I'm slightly cheating with my testing of this excerpt, but it works!
137
131
-->
138
132
139
- In practice, you don't need to write ` self ` in your code very often.
140
- If you don't explicitly write ` self ` ,
141
- Swift assumes that you are referring to a property or method of the current instance
142
- whenever you use a known property or method name within a method.
143
- This assumption is demonstrated by the use of ` count ` (rather than ` self.count ` )
144
- inside the three instance methods for ` Counter ` .
133
+ 实际上,在代码中你不需要经常写 ` self ` 。
134
+ 如果你没有显式地写出 ` self ` ,Swift会假定你在方法内部使用已知的属性或方法名时,指的是当前实例的属性或方法。
135
+ 这种假设在 ` Counter ` 类的三个实例方法中通过直接使用 ` count ` (而非 ` self.count ` )得到了体现。
145
136
146
- The main exception to this rule occurs when a parameter name for an instance method
147
- has the same name as a property of that instance.
148
- In this situation, the parameter name takes precedence,
149
- and it becomes necessary to refer to the property in a more qualified way.
150
- You use the ` self ` property to
151
- distinguish between the parameter name and the property name.
137
+ 此规则的主要例外情况发生在实例方法的形参名称与该实例的属性名称相同时。
138
+ 在这种情况下,形参名称优先,此时就需要以更明确的方式引用属性。你可以使用 ` self ` 属性来区分形参名称和属性名称。
152
139
153
- Here, ` self ` disambiguates between
154
- a method parameter called ` x ` and an instance property that's also called ` x ` :
140
+ 在如下情况中,` self ` 用于区分一个名为 x 的方法形参和一个同样名为 x 的实例属性:
155
141
156
142
``` swift
157
143
struct Point {
@@ -185,29 +171,21 @@ if somePoint.isToTheRightOf(x: 1.0) {
185
171
```
186
172
-->
187
173
188
- Without the ` self ` prefix,
189
- Swift would assume that both uses of ` x ` referred to the method parameter called ` x ` .
174
+ 如果没有 ` self ` 前缀,Swift 会假定 ` x ` 的两个使用都指的是名为 ` x ` 的方法形参。
190
175
191
- ### Modifying Value Types from Within Instance Methods
176
+ ### 从实例方法内部修改值类型
192
177
193
- Structures and enumerations are * value types* .
194
- By default, the properties of a value type can't be modified from within its instance methods.
178
+ 结构体和枚举是 * 值类型* 。默认情况下,值类型的属性不能在其实例方法内部被修改。
195
179
196
180
<!--
197
181
TODO: find out why. once I actually know why, explain it.
198
182
-->
199
183
200
- However, if you need to modify the properties of your structure or enumeration
201
- within a particular method,
202
- you can opt in to * mutating* behavior for that method.
203
- The method can then mutate (that is, change)
204
- its properties from within the method,
205
- and any changes that it makes are written back to the original structure when the method ends.
206
- The method can also assign a completely new instance to its implicit ` self ` property,
207
- and this new instance will replace the existing one when the method ends.
184
+ 然而,如果你需要在特定方法内部修改结构体或枚举的属性,你可以为该方法启用 * mutating* 行为。
185
+ 这样方法就可以在内部改变其属性,并且所有的更改在方法结束时会写回到原始结构体中。
186
+ 该方法还可以将一个全新的实例赋值给隐式的 ` self ` 属性,并且这个新实例将在方法结束时替换现有实例。
208
187
209
- You can opt in to this behavior by placing the ` mutating ` keyword
210
- before the ` func ` keyword for that method:
188
+ 你可以通过在该方法的 ` func ` 关键字前添加 ` mutating ` 关键字来启用这种行为:
211
189
212
190
``` swift
213
191
struct Point {
@@ -241,16 +219,11 @@ print("The point is now at (\(somePoint.x), \(somePoint.y))")
241
219
```
242
220
-->
243
221
244
- The ` Point ` structure above defines a mutating ` moveBy(x:y:) ` method,
245
- which moves a ` Point ` instance by a certain amount.
246
- Instead of returning a new point,
247
- this method actually modifies the point on which it's called.
248
- The ` mutating ` keyword is added to its definition
249
- to enable it to modify its properties.
222
+ 上面的 ` Point ` 结构体定义了一个 mutating 的 ` moveBy(x:y:) ` 方法,该方法可以将一个 ` Point ` 实例移动一定的距离。
223
+ 这个方法直接修改其调用的点,而不是返回一个新的点。为了使该方法能够修改其属性,` mutating ` 关键字被添加到它的定义中。
250
224
251
- Note that you can't call a mutating method on a constant of structure type,
252
- because its properties can't be changed, even if they're variable properties,
253
- as described in < doc:Properties#Stored-Properties-of-Constant-Structure-Instances > :
225
+ 请注意,你不能在结构体类型的常量上调用 mutating 方法,因为其属性不能被改变,即使它们是可变属性,
226
+ 参见:< doc:Properties#Stored-Properties-of-Constant-Structure-Instances >
254
227
255
228
``` swift
256
229
let fixedPoint = Point (x : 3.0 , y : 3.0 )
@@ -289,10 +262,9 @@ fixedPoint.moveBy(x: 2.0, y: 3.0)
289
262
if the setter for the computed property is explicitly defined as @!mutating.
290
263
-->
291
264
292
- ### Assigning to self Within a Mutating Method
265
+ ### 在 mutating 方法中给 self 赋值
293
266
294
- Mutating methods can assign an entirely new instance to the implicit ` self ` property.
295
- The ` Point ` example shown above could have been written in the following way instead:
267
+ mutating 方法可以将一个全新的实例赋值给隐式的 ` self ` 属性。上面的 ` Point ` 示例可以改写为以下方式:
296
268
297
269
``` swift
298
270
struct Point {
@@ -320,13 +292,10 @@ struct Point {
320
292
```
321
293
-->
322
294
323
- This version of the mutating ` moveBy(x:y:) ` method creates a new structure
324
- whose ` x ` and ` y ` values are set to the target location.
325
- The end result of calling this alternative version of the method
326
- will be exactly the same as for calling the earlier version.
295
+ 这个版本的 mutating ` moveBy(x:y:) ` 方法创建了一个新的结构体,其 ` x ` 和 ` y ` 值被设置为目标位置。
296
+ 调用这个替代版本方法与调用早期版本方法的最终结果完全相同。
327
297
328
- Mutating methods for enumerations can set the implicit ` self ` parameter to be
329
- a different case from the same enumeration:
298
+ 枚举的 mutating 方法可以将隐式的 ` self ` 参数设置为同一枚举的不同case:
330
299
331
300
``` swift
332
301
enum TriStateSwitch {
@@ -374,10 +343,7 @@ ovenLight.next()
374
343
```
375
344
-->
376
345
377
- This example defines an enumeration for a three-state switch.
378
- The switch cycles between three different power states
379
- (` off ` , ` low ` and ` high ` )
380
- every time its ` next() ` method is called.
346
+ 这个示例定义了一个三状态开关的枚举。每次调用其 ` next() ` 方法时,开关将在三种不同的电源状态(` off ` , ` low ` 和 ` high ` )之间循环切换。
381
347
382
348
## Type Methods
383
349
0 commit comments