修复枚举中的.
This commit is contained in:
@ -9,10 +9,10 @@
|
|||||||
> 翻译+校对:[DianQK](https://github.com/DianQK)
|
> 翻译+校对:[DianQK](https://github.com/DianQK)
|
||||||
|
|
||||||
> 2.1
|
> 2.1
|
||||||
> 翻译:[DianQK](https://github.com/DianQK),[Realank](https://github.com/Realank) 校对:[shanks](http://codebuild.me),2016-01-18
|
> 翻译:[DianQK](https://github.com/DianQK),[Realank](https://github.com/Realank) 校对:[shanks](http://codebuild.me),2016-01-18
|
||||||
>
|
>
|
||||||
> 2.2
|
> 2.2
|
||||||
> 校对:[SketchK](https://github.com/SketchK) 2016-05-13
|
> 校对:[SketchK](https://github.com/SketchK) 2016-05-13
|
||||||
> 3.0.1,shanks,2016-11-13
|
> 3.0.1,shanks,2016-11-13
|
||||||
|
|
||||||
本页包含内容:
|
本页包含内容:
|
||||||
@ -34,17 +34,17 @@
|
|||||||
下面的例子,定义一个很简单的`Counter`类,`Counter`能被用来对一个动作发生的次数进行计数:
|
下面的例子,定义一个很简单的`Counter`类,`Counter`能被用来对一个动作发生的次数进行计数:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class Counter {
|
class Counter {
|
||||||
var count = 0
|
var count = 0
|
||||||
func increment() {
|
func increment() {
|
||||||
count += 1
|
count += 1
|
||||||
}
|
}
|
||||||
func increment(by amount: Int) {
|
func increment(by amount: Int) {
|
||||||
count += amount
|
count += amount
|
||||||
}
|
}
|
||||||
func reset() {
|
func reset() {
|
||||||
count = 0
|
count = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -62,15 +62,15 @@ let counter = Counter()
|
|||||||
// 初始计数值是0
|
// 初始计数值是0
|
||||||
counter.increment()
|
counter.increment()
|
||||||
// 计数值现在是1
|
// 计数值现在是1
|
||||||
counter.increment(by: 5)
|
counter.increment(by: 5)
|
||||||
// 计数值现在是6
|
// 计数值现在是6
|
||||||
counter.reset()
|
counter.reset()
|
||||||
// 计数值现在是0
|
// 计数值现在是0
|
||||||
```
|
```
|
||||||
|
|
||||||
函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),详情参见[指定外部参数名](./06_Functions.html#specifying_external_parameter_names)。方法参数也一样,因为方法就是函数,只是这个函数与某个类型相关联了。
|
函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用),详情参见[指定外部参数名](./06_Functions.html#specifying_external_parameter_names)。方法参数也一样,因为方法就是函数,只是这个函数与某个类型相关联了。
|
||||||
|
|
||||||
|
|
||||||
<a name="the_self_property"></a>
|
<a name="the_self_property"></a>
|
||||||
### self 属性
|
### self 属性
|
||||||
|
|
||||||
@ -104,8 +104,8 @@ if somePoint.isToTheRightOfX(1.0) {
|
|||||||
// 打印 "This point is to the right of the line where x == 1.0"
|
// 打印 "This point is to the right of the line where x == 1.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
如果不使用`self`前缀,Swift 就认为两次使用的`x`都指的是名称为`x`的函数参数。
|
如果不使用`self`前缀,Swift 就认为两次使用的`x`都指的是名称为`x`的函数参数。
|
||||||
|
|
||||||
<a name="modifying_value_types_from_within_instance_methods"></a>
|
<a name="modifying_value_types_from_within_instance_methods"></a>
|
||||||
### 在实例方法中修改值类型
|
### 在实例方法中修改值类型
|
||||||
|
|
||||||
@ -137,19 +137,19 @@ print("The point is now at (\(somePoint.x), \(somePoint.y))")
|
|||||||
let fixedPoint = Point(x: 3.0, y: 3.0)
|
let fixedPoint = Point(x: 3.0, y: 3.0)
|
||||||
fixedPoint.moveByX(2.0, y: 3.0)
|
fixedPoint.moveByX(2.0, y: 3.0)
|
||||||
// 这里将会报告一个错误
|
// 这里将会报告一个错误
|
||||||
```
|
```
|
||||||
|
|
||||||
<a name="assigning_to_self_within_a_mutating_method"></a>
|
<a name="assigning_to_self_within_a_mutating_method"></a>
|
||||||
### 在可变方法中给 self 赋值
|
### 在可变方法中给 self 赋值
|
||||||
|
|
||||||
可变方法能够赋给隐含属性`self`一个全新的实例。上面`Point`的例子可以用下面的方式改写:
|
可变方法能够赋给隐含属性`self`一个全新的实例。上面`Point`的例子可以用下面的方式改写:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
struct Point {
|
struct Point {
|
||||||
var x = 0.0, y = 0.0
|
var x = 0.0, y = 0.0
|
||||||
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
|
mutating func moveBy(x deltaX: Double, y deltaY: Double) {
|
||||||
self = Point(x: x + deltaX, y: y + deltaY)
|
self = Point(x: x + deltaX, y: y + deltaY)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -162,12 +162,12 @@ enum TriStateSwitch {
|
|||||||
case Off, Low, High
|
case Off, Low, High
|
||||||
mutating func next() {
|
mutating func next() {
|
||||||
switch self {
|
switch self {
|
||||||
case Off:
|
case .Off:
|
||||||
self = Low
|
self = .Low
|
||||||
case Low:
|
case .Low:
|
||||||
self = High
|
self = .High
|
||||||
case High:
|
case .High:
|
||||||
self = Off
|
self = .Off
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,27 +208,27 @@ SomeClass.someTypeMethod()
|
|||||||
游戏初始时,所有的游戏等级(除了等级 1)都被锁定。每次有玩家完成一个等级,这个等级就对这个设备上的所有玩家解锁。`LevelTracker`结构体用类型属性和方法监测游戏的哪个等级已经被解锁。它还监测每个玩家的当前等级。
|
游戏初始时,所有的游戏等级(除了等级 1)都被锁定。每次有玩家完成一个等级,这个等级就对这个设备上的所有玩家解锁。`LevelTracker`结构体用类型属性和方法监测游戏的哪个等级已经被解锁。它还监测每个玩家的当前等级。
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
struct LevelTracker {
|
struct LevelTracker {
|
||||||
static var highestUnlockedLevel = 1
|
static var highestUnlockedLevel = 1
|
||||||
var currentLevel = 1
|
var currentLevel = 1
|
||||||
|
|
||||||
static func unlock(_ level: Int) {
|
static func unlock(_ level: Int) {
|
||||||
if level > highestUnlockedLevel { highestUnlockedLevel = level }
|
if level > highestUnlockedLevel { highestUnlockedLevel = level }
|
||||||
}
|
}
|
||||||
|
|
||||||
static func isUnlocked(_ level: Int) -> Bool {
|
static func isUnlocked(_ level: Int) -> Bool {
|
||||||
return level <= highestUnlockedLevel
|
return level <= highestUnlockedLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
mutating func advance(to level: Int) -> Bool {
|
mutating func advance(to level: Int) -> Bool {
|
||||||
if LevelTracker.isUnlocked(level) {
|
if LevelTracker.isUnlocked(level) {
|
||||||
currentLevel = level
|
currentLevel = level
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -243,16 +243,16 @@ struct LevelTracker {
|
|||||||
下面,`Player`类使用`LevelTracker`来监测和更新每个玩家的发展进度:
|
下面,`Player`类使用`LevelTracker`来监测和更新每个玩家的发展进度:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
class Player {
|
class Player {
|
||||||
var tracker = LevelTracker()
|
var tracker = LevelTracker()
|
||||||
let playerName: String
|
let playerName: String
|
||||||
func complete(level: Int) {
|
func complete(level: Int) {
|
||||||
LevelTracker.unlock(level + 1)
|
LevelTracker.unlock(level + 1)
|
||||||
tracker.advance(to: level + 1)
|
tracker.advance(to: level + 1)
|
||||||
}
|
}
|
||||||
init(name: String) {
|
init(name: String) {
|
||||||
playerName = name
|
playerName = name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -261,8 +261,8 @@ class Player {
|
|||||||
你还可以为一个新的玩家创建一个`Player`的实例,然后看这个玩家完成等级一时发生了什么:
|
你还可以为一个新的玩家创建一个`Player`的实例,然后看这个玩家完成等级一时发生了什么:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
var player = Player(name: "Argyrios")
|
var player = Player(name: "Argyrios")
|
||||||
player.complete(level: 1)
|
player.complete(level: 1)
|
||||||
print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
||||||
// 打印 "highest unlocked level is now 2"
|
// 打印 "highest unlocked level is now 2"
|
||||||
```
|
```
|
||||||
@ -270,11 +270,11 @@ print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
|
|||||||
如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,那么试图设置玩家当前等级将会失败:
|
如果你创建了第二个玩家,并尝试让他开始一个没有被任何玩家解锁的等级,那么试图设置玩家当前等级将会失败:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
player = Player(name: "Beto")
|
player = Player(name: "Beto")
|
||||||
if player.tracker.advance(to: 6) {
|
if player.tracker.advance(to: 6) {
|
||||||
print("player is now on level 6")
|
print("player is now on level 6")
|
||||||
} else {
|
} else {
|
||||||
print("level 6 has not yet been unlocked")
|
print("level 6 has not yet been unlocked")
|
||||||
}
|
}
|
||||||
// 打印 "level 6 has not yet been unlocked"
|
// 打印 "level 6 has not yet been unlocked"
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user