From ca36e6a9a0cc07c13c3ef57c17ee12187a47c625 Mon Sep 17 00:00:00 2001 From: Xwoder Date: Thu, 13 Aug 2015 08:31:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E5=92=8C=E5=AD=97=E7=AC=A6=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/03_Strings_and_Characters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/chapter2/03_Strings_and_Characters.md b/source/chapter2/03_Strings_and_Characters.md index 0084d44a..abf9ab8a 100755 --- a/source/chapter2/03_Strings_and_Characters.md +++ b/source/chapter2/03_Strings_and_Characters.md @@ -114,7 +114,7 @@ Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字 您可通过`for-in`循环来遍历字符串中的`characters`属性来获取每一个字符的值: ```swift -for character in "Dog!🐶" { +for character in "Dog!🐶".characters { print(character) } // D From 9e730a4a4208321dc9e7d44b8cdaa829a889c44a Mon Sep 17 00:00:00 2001 From: Xwoder Date: Thu, 13 Aug 2015 21:03:42 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/03_Strings_and_Characters.md | 29 +++++++++----------- source/chapter2/04_Collection_Types.md | 2 +- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/source/chapter2/03_Strings_and_Characters.md b/source/chapter2/03_Strings_and_Characters.md index abf9ab8a..7b780c20 100755 --- a/source/chapter2/03_Strings_and_Characters.md +++ b/source/chapter2/03_Strings_and_Characters.md @@ -276,13 +276,12 @@ let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}" ## 计算字符数量 (Counting Characters) -如果想要获得一个字符串中字符的数量,可以调用全局函数`count(_:)`,把字符串作为参数传进去: - +如果想要获得一个字符串中字符的数量,可以使用字符串的characters属性的count属性: ```swift let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪" -println("unusualMenagerie has \(count(unusualMenagerie)) characters") -// 打印输出:"unusualMenagerie has 40 characters" +print("unusualMenagerie has \(unusualMenagerie.characters.count) characters") +// 打印输出 "unusualMenagerie has 40 characters" ``` 注意在 Swift 中,使用可拓展的字符群集作为字符来连接或改变字符串时,并不一定会更改字符串的字符数量。 @@ -291,13 +290,13 @@ println("unusualMenagerie has \(count(unusualMenagerie)) characters") ```swift var word = "cafe" -println("the number of characters in \(word) is \(count(word))") +print("the number of characters in \(word) is \(word.characters.count)") // 打印输出 "the number of characters in cafe is 4" - + word += "\u{301}" // COMBINING ACUTE ACCENT, U+0301 - -println("the number of characters in \(word) is \(count(word))") -// 打印输出 "the number of characters in café is 4" + +print("the number of characters in \(word) is \(word.characters.count)") +// 打印输出 "the number of characters in café is 4" ``` > 注意: @@ -354,12 +353,10 @@ greeting.endIndex.successor() // 错误 使用全局函数`indices`会创建一个包含全部索引的范围(`Range`),用来在一个字符串中访问分立的字符。 ```swift -for index in indices(greeting) { - print("\(greeting[index]) ") +for index in greeting.characters.indices { + print("\(greeting[index]) ", appendNewline: false) } -println("\n") -// prints "G u t e n T a g" - +// prints "G u t e n T a g !" ``` @@ -565,9 +562,9 @@ let dogString = "Dog‼🐶" ```swift for codeUnit in dogString.utf8 { - print("\(codeUnit) ") + print("\(codeUnit) ", appendNewline: false) } -print("\n") +print("") // 68 111 103 226 128 188 240 159 144 182 ``` diff --git a/source/chapter2/04_Collection_Types.md b/source/chapter2/04_Collection_Types.md index c93fa22e..f26b2ccb 100755 --- a/source/chapter2/04_Collection_Types.md +++ b/source/chapter2/04_Collection_Types.md @@ -181,7 +181,7 @@ shoppingList[4...6] = ["Bananas", "Apples"] ``` > 注意: -> 不可以用下表访问的形式去在数组尾部添加新项。 +> 不可以用下标访问的形式去在数组尾部添加新项。 调用数组的`insert(_:atIndex:)`方法来在某个具体索引值之前添加数据项: From b3403c2bf88bc79c7a1b9e3e8d665ad0b7ae7719 Mon Sep 17 00:00:00 2001 From: Xwoder Date: Sat, 15 Aug 2015 10:10:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/chapter2/05_Control_Flow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/chapter2/05_Control_Flow.md b/source/chapter2/05_Control_Flow.md index d3a31cf2..c189334b 100755 --- a/source/chapter2/05_Control_Flow.md +++ b/source/chapter2/05_Control_Flow.md @@ -144,7 +144,7 @@ print("The loop statements were executed \(index) times") `while`循环运行一系列语句直到条件变成`false`。这类循环适合使用在第一次迭代前迭代次数未知的情况下。Swift 提供两种`while`循环形式: * `while`循环,每次在循环开始时计算条件是否符合; -* `do-while`循环,每次在循环结束时计算条件是否符合。 +* `repeat-while`循环,每次在循环结束时计算条件是否符合。 ###While