From 9e730a4a4208321dc9e7d44b8cdaa829a889c44a Mon Sep 17 00:00:00 2001 From: Xwoder Date: Thu, 13 Aug 2015 21:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=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 | 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:)`方法来在某个具体索引值之前添加数据项: