更新部分代码

This commit is contained in:
Xwoder
2015-08-13 21:03:42 +08:00
parent ca36e6a9a0
commit 9e730a4a42
2 changed files with 14 additions and 17 deletions

View File

@ -276,13 +276,12 @@ let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
<a name="counting_characters"></a> <a name="counting_characters"></a>
## 计算字符数量 (Counting Characters) ## 计算字符数量 (Counting Characters)
如果想要获得一个字符串中字符的数量,可以调用全局函数`count(_:)`,把字符串作为参数传进去 如果想要获得一个字符串中字符的数量,可以使用字符串的characters属性的count属性
```swift ```swift
let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪" let unusualMenagerie = "Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"
println("unusualMenagerie has \(count(unusualMenagerie)) characters") print("unusualMenagerie has \(unusualMenagerie.characters.count) characters")
// 打印输出"unusualMenagerie has 40 characters" // 打印输出 "unusualMenagerie has 40 characters"
``` ```
注意在 Swift 中,使用可拓展的字符群集作为字符来连接或改变字符串时,并不一定会更改字符串的字符数量。 注意在 Swift 中,使用可拓展的字符群集作为字符来连接或改变字符串时,并不一定会更改字符串的字符数量。
@ -291,13 +290,13 @@ println("unusualMenagerie has \(count(unusualMenagerie)) characters")
```swift ```swift
var word = "cafe" 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" // 打印输出 "the number of characters in cafe is 4"
word += "\u{301}" // COMBINING ACUTE ACCENT, U+0301 word += "\u{301}" // COMBINING ACUTE ACCENT, U+0301
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 caf is 4" // 打印输出 "the number of characters in café is 4"
``` ```
> 注意: > 注意:
@ -354,12 +353,10 @@ greeting.endIndex.successor() // 错误
使用全局函数`indices`会创建一个包含全部索引的范围(`Range`),用来在一个字符串中访问分立的字符。 使用全局函数`indices`会创建一个包含全部索引的范围(`Range`),用来在一个字符串中访问分立的字符。
```swift ```swift
for index in indices(greeting) { for index in greeting.characters.indices {
print("\(greeting[index]) ") 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"
``` ```
<a name="inserting_and_removing"></a> <a name="inserting_and_removing"></a>
@ -565,9 +562,9 @@ let dogString = "Dog‼🐶"
```swift ```swift
for codeUnit in dogString.utf8 { for codeUnit in dogString.utf8 {
print("\(codeUnit) ") print("\(codeUnit) ", appendNewline: false)
} }
print("\n") print("")
// 68 111 103 226 128 188 240 159 144 182 // 68 111 103 226 128 188 240 159 144 182
``` ```

View File

@ -181,7 +181,7 @@ shoppingList[4...6] = ["Bananas", "Apples"]
``` ```
> 注意: > 注意:
> 不可以用下访问的形式去在数组尾部添加新项。 > 不可以用下访问的形式去在数组尾部添加新项。
调用数组的`insert(_:atIndex:)`方法来在某个具体索引值之前添加数据项: 调用数组的`insert(_:atIndex:)`方法来在某个具体索引值之前添加数据项: