Skip to content

Commit 745a510

Browse files
committed
Merge pull request CocoaChina-editors#108 from likang/fix
校对
2 parents 9eadf4b + baaceef commit 745a510

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

The Swift Programming Language/01Welcome to Swift/02A Swift Tour.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ let explicitDouble: Double = 70
6363

6464
要创建一个空数组或者字典,使用初始化语法。
6565

66-
let emptyArray = String[]()
67-
let emptyDictionary = Dictionary<String, Float>()
66+
let emptyArray = [String]()
67+
let emptyDictionary = [String: Float]()
6868

6969
如果类型信息可以被推断出来,你可以用`[]``[:]`来创建空数组和空字典——就像你声明变量或者给函数传参数的时候一样。
7070

71-
shoppingList = [] // 去逛街并买点东西
71+
shoppingList = []
72+
occupations = [:]
7273

7374
## 控制流
7475

@@ -87,7 +88,7 @@ let explicitDouble: Double = 70
8788

8889
`if`语句中,条件必须是一个布尔表达式——像`if score { ... }`这样的代码是错误的。
8990

90-
你可以一起使用`if``let`来处理值缺失的情况。有些变量的值是可选的。一个可选的值可能是一个具体的值或者是`nil`,表示值缺失。在类型后面加一个问号来标记这个变量的值是可选的。
91+
你可以同时使用`if``let`来处理值缺失的情况。有些变量的值是可选的。一个可选的值可能是一个具体的值或者是`nil`,表示值缺失。在类型后面加一个问号来标记这个变量的值是可选的。
9192

9293
var optionalString: String? = "Hello"
9394
optionalString == nil
@@ -102,7 +103,7 @@ let explicitDouble: Double = 70
102103
103104
如果变量的可选值是`nil`,条件会判断为`false`,大括号中的代码会被跳过。如果不是`nil`,会将值赋给`let`后面的常量,这样代码块中就可以使用这个值了。
104105

105-
`switch`支持任意类型的数据以及各种比较操作——不仅仅是整数以及测试相等
106+
`switch`支持任意类型的数据以及各种比较操作——不限于整数或等值测试
106107

107108
let vegetable = "red pepper"
108109
switch vegetable {
@@ -205,7 +206,7 @@ let explicitDouble: Double = 70
205206

206207
func returnFifteen() -> Int {
207208
var y = 10
208-
func add() {
209+
func add() {
209210
y += 5
210211
}
211212
add()
@@ -246,13 +247,13 @@ let explicitDouble: Double = 70
246247
(number: Int) -> Int in
247248
let result = 3 * number
248249
return result
249-
})
250+
})
250251

251252
> 练习:重写闭包,对所有奇数返回0.
252253
253254
有很多种创建闭包的方法。如果一个闭包的类型已知,比如作为一个回调函数,你可以忽略参数的类型和返回值。单个语句闭包会把它语句的值当做结果返回。
254255

255-
你可以通过参数位置而不是参数名字来引用参数——这个方法在非常短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在括号后面。
256+
你可以通过参数位置而不是参数名字来引用参数——这个方法在很短的闭包中非常有用。当一个闭包作为最后一个参数传给一个函数的时候,它可以直接跟在括号后面。
256257

257258
sort([1, 5, 3, 12, 2]) { $0 > $1 }
258259

@@ -333,12 +334,12 @@ let explicitDouble: Double = 70
333334
}
334335

335336
var perimeter: Double {
336-
get {
337-
return 3.0 * sideLength
338-
}
339-
set {
340-
sideLength = newValue / 3.0
341-
}
337+
get {
338+
return 3.0 * sideLength
339+
}
340+
set {
341+
sideLength = newValue / 3.0
342+
}
342343
}
343344

344345
override func simpleDescription() -> String {
@@ -364,14 +365,14 @@ let explicitDouble: Double = 70
364365

365366
class TriangleAndSquare {
366367
var triangle: EquilateralTriangle {
367-
willSet {
368-
square.sideLength = newValue.sideLength
369-
}
368+
willSet {
369+
square.sideLength = newValue.sideLength
370+
}
370371
}
371372
var square: Square {
372-
willSet {
373-
triangle.sideLength = newValue.sideLength
374-
}
373+
willSet {
374+
triangle.sideLength = newValue.sideLength
375+
}
375376
}
376377
init(size: Double, name: String) {
377378
square = Square(sideLength: size, name: name)
@@ -563,10 +564,10 @@ let explicitDouble: Double = 70
563564

564565
在尖括号里写一个名字来创建一个泛型函数或者类型。
565566

566-
func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {
567-
var result = ItemType[]()
568-
for i in 0..times {
569-
result += item
567+
func repeat<Item>(item: Item, times: Int) -> [Item] {
568+
var result = [Item]()
569+
for i in 0..<times {
570+
result.append(item)
570571
}
571572
return result
572573
}

0 commit comments

Comments
 (0)