From 425fae6ddf5a85837bd1398c3cc0198e146f44f3 Mon Sep 17 00:00:00 2001 From: Kevin Jiang Date: Sat, 27 Sep 2014 23:24:31 +0800 Subject: [PATCH] =?UTF-8?q?Swift=E6=9C=89=E6=9B=B4=E6=96=B0=EF=BC=8C?= =?UTF-8?q?=E8=AF=AD=E6=B3=95=E6=9C=89=E4=BA=9B=E4=B8=8D=E5=A4=AA=E4=B8=80?= =?UTF-8?q?=E6=A0=B7=EF=BC=8C=E5=9C=A8=E5=AD=A6=E4=B9=A0=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=E5=81=9A=E4=BA=86=E4=B8=80=E4=BA=9B=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/18_Type_Casting.md | 2 +- source/chapter2/20_Extensions.md | 9 +++++---- source/chapter2/21_Protocols.md | 14 +++++++------- source/chapter2/22_Generics.md | 10 +++++----- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/source/chapter2/18_Type_Casting.md b/source/chapter2/18_Type_Casting.md index 1f402a74..14e69676 100755 --- a/source/chapter2/18_Type_Casting.md +++ b/source/chapter2/18_Type_Casting.md @@ -16,7 +16,7 @@ _类型转换_可以判断实例的类型,也可以将实例看做是其父类 类型转换在 Swift 中使用`is` 和 `as`操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。 -你也可以用来检查一个类是否实现了某个协议,就像在 [Checking for Protocol Conformance](21_Protocols.html#checking_for_protocol_conformance)部分讲述的一样。 +你也可以用来检查一个类是否实现了某个协议,就像在 [Checking for Protocol Conformance](Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-XID_363)部分讲述的一样。 ## 定义一个类层次作为例子 diff --git a/source/chapter2/20_Extensions.md b/source/chapter2/20_Extensions.md index cd014f1e..72e094c8 100755 --- a/source/chapter2/20_Extensions.md +++ b/source/chapter2/20_Extensions.md @@ -156,7 +156,7 @@ let centerRect = Rect(center: Point(x: 4.0, y: 4.0), ```swift extension Int { func repetitions(task: () -> ()) { - for i in 0..self { + for i in 0.. Int { + subscript(var digitIndex: Int) -> Int { var decimalBase = 1 - for _ in 1...digitIndex { + while digitIndex > 0 { decimalBase *= 10 + --digitIndex } - return (self / decimalBase) % 10 + return (self / decimalBase) % 10 } } 746381295[0] diff --git a/source/chapter2/21_Protocols.md b/source/chapter2/21_Protocols.md index ad0bcaa4..499f7dd0 100755 --- a/source/chapter2/21_Protocols.md +++ b/source/chapter2/21_Protocols.md @@ -112,7 +112,7 @@ class Starship: FullyNamed { self.prefix = prefix } var fullName: String { - return (prefix ? prefix! + " " : " ") + name + return (prefix != nil ? prefix! + " " : " ") + name } } var ncc1701 = Starship(name: "Enterprise", prefix: "USS") @@ -349,9 +349,9 @@ class SnakesAndLadders: DiceGame { let finalSquare = 25 let dice = Dice(sides: 6, generator: LinearCongruentialGenerator()) var square = 0 - var board: Int[] + var board: [Int] init() { - board = Int[](count: finalSquare + 1, repeatedValue: 0) + board = [Int](count: finalSquare + 1, repeatedValue: 0) board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02 board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08 } @@ -508,7 +508,7 @@ println(somethingTextRepresentable.asText()) 协议类型可以被集合使用,表示集合中的元素均为协议类型: ```swift -let things: TextRepresentable[] = [game,d12,simonTheHamster] +let things: [TextRepresentable] = [game,d12,simonTheHamster] ``` 如下所示,`things`数组可以被直接遍历,并调用其中元素的`asText()`函数: @@ -675,7 +675,7 @@ class Animal { `Circle,Country,Animal`并没有一个相同的基类,因而采用`AnyObject`类型的数组来装载在他们的实例,如下所示: ```swift -let objects: AnyObject[] = [ +let objects: [AnyObject] = [ Circle(radius: 2.0), Country(area: 243_610), Animal(legs: 4) @@ -718,8 +718,8 @@ for object in objects { ```swift @objc protocol CounterDataSource { - @optional func incrementForCount(count: Int) -> Int - @optional var fixedIncrement: Int { get } + optional func incrementForCount(count: Int) -> Int + optional var fixedIncrement: Int { get } } ``` diff --git a/source/chapter2/22_Generics.md b/source/chapter2/22_Generics.md index ef69d439..12b40044 100755 --- a/source/chapter2/22_Generics.md +++ b/source/chapter2/22_Generics.md @@ -180,7 +180,7 @@ struct IntStack { ```swift struct Stack { - var items = T[]() + var items = [T]() mutating func push(item: T) { items.append(item) } @@ -254,7 +254,7 @@ func someFunction(someT: T, someU: U) { 这里有个名为`findStringIndex`的非泛型函数,该函数功能是去查找包含一给定`String`值的数组。若查找到匹配的字符串,`findStringIndex`函数返回该字符串在数组中的索引值(`Int`),反之则返回`nil`: ```swift -func findStringIndex(array: String[], valueToFind: String) -> Int? { +func findStringIndex(array: [String], valueToFind: String) -> Int? { for (index, value) in enumerate(array) { if value == valueToFind { return index @@ -356,7 +356,7 @@ protocol Container { ```swift struct IntStack: Container { // IntStack的原始实现 - var items = Int[]() + var items = [Int]() mutating func push(item: Int) { items.append(item) } @@ -389,7 +389,7 @@ struct IntStack: Container { ```swift struct Stack: Container { // original Stack implementation - var items = T[]() + var items = [T]() mutating func push(item: T) { items.append(item) } @@ -447,7 +447,7 @@ func allItemsMatch< } // 检查两个Container相应位置的元素彼此是否相等 - for i in 0..someContainer.count { + for i in 0..