Merge remote-tracking branch 'numbbbbb/gh-pages'
This commit is contained in:
@ -114,7 +114,7 @@ Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字
|
|||||||
您可通过`for-in`循环来遍历字符串中的`characters`属性来获取每一个字符的值:
|
您可通过`for-in`循环来遍历字符串中的`characters`属性来获取每一个字符的值:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
for character in "Dog!🐶" {
|
for character in "Dog!🐶".characters {
|
||||||
print(character)
|
print(character)
|
||||||
}
|
}
|
||||||
// D
|
// D
|
||||||
@ -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
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -181,7 +181,7 @@ shoppingList[4...6] = ["Bananas", "Apples"]
|
|||||||
```
|
```
|
||||||
|
|
||||||
> 注意:
|
> 注意:
|
||||||
> 不可以用下表访问的形式去在数组尾部添加新项。
|
> 不可以用下标访问的形式去在数组尾部添加新项。
|
||||||
|
|
||||||
|
|
||||||
调用数组的`insert(_:atIndex:)`方法来在某个具体索引值之前添加数据项:
|
调用数组的`insert(_:atIndex:)`方法来在某个具体索引值之前添加数据项:
|
||||||
|
|||||||
@ -144,7 +144,7 @@ print("The loop statements were executed \(index) times")
|
|||||||
`while`循环运行一系列语句直到条件变成`false`。这类循环适合使用在第一次迭代前迭代次数未知的情况下。Swift 提供两种`while`循环形式:
|
`while`循环运行一系列语句直到条件变成`false`。这类循环适合使用在第一次迭代前迭代次数未知的情况下。Swift 提供两种`while`循环形式:
|
||||||
|
|
||||||
* `while`循环,每次在循环开始时计算条件是否符合;
|
* `while`循环,每次在循环开始时计算条件是否符合;
|
||||||
* `do-while`循环,每次在循环结束时计算条件是否符合。
|
* `repeat-while`循环,每次在循环结束时计算条件是否符合。
|
||||||
|
|
||||||
<a name="while"></a>
|
<a name="while"></a>
|
||||||
###While
|
###While
|
||||||
|
|||||||
Reference in New Issue
Block a user