<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Swift `for-in`循環 > 原文: [https://www.programiz.com/swift-programming/for-in-loop](https://www.programiz.com/swift-programming/for-in-loop) #### 在本文中,您將了解`for-in`循環,其用例和變體。 `For-in`循環用于運行一組任務一定次數。 這些循環遍歷任何序列,例如數組中的項目,范圍或字符串中的字符。 我們還使用`for-in`循環在固定的時間內執行一些重復的過程。 * * * ## 為什么需要`for in`循環? 想象有人告訴您編寫一個輸出`"Hello, World!"`的程序! 在屏幕上。 您的解決方案將是: ```swift print("Hello, World!") ``` 如果他們改變主意并告訴您編寫一個在屏幕上顯示五次`"Hello, World!"`的程序,該怎么辦? 如果沒有循環知識,您的解決方案可能是: ```swift print("Hello, World!") print("Hello, World!") print("Hello, World!") print("Hello, World!") print("Hello, World!") ``` 嗯,完成五次相似的代碼來完成相同的工作看起來很耗時。 如果是這樣,如果有人要求您編寫一個在屏幕上輸出數百甚至上百萬次`"Hello, World!"`的程序,您將怎么辦? 一種幼稚的解決方案是將`print`語句寫入給定的次數。 聽起來很瘋狂吧? 但是,使用`for-in`循環和以下幾行代碼可以找到更好的解決方案: ```swift for i in 1...100 { //outputs Hello world for 100 times in the screen print("Hello, World!") } ``` 不用擔心語法,我們將在下面進行探討。 * * * ## `for in`循環語法 您可以在 Swift 中創建一個`for in`循環,如下所示: ```swift for <value> in <range> { <some work here> } ``` 上面的循環遍歷一個范圍,我們可以在`<value>`變量中訪問從該范圍返回的每個元素。 如果您不了解范圍,則可以查看以下文章: [Swift 范圍](/swift-programming/ranges "Swift ranges")。 * * * ## 它是如何工作的? * 被迭代的序列是`<range>`。 * `<value>`設置為該范圍內的第一個數字,并且執行**循環內部的語句**。 * 執行該語句后,`<value>`被更新為`<range>`語句包含的第二個值,`<some work here>`再次被執行。 * 該過程一直持續到到達范圍的終點并停止循環。 ### 示例 1:`for-in`循環在 Swift 中的工作方式 ```swift for i in 1...3 { print("Hello world!. Value is \(i)") } ``` 運行該程序時,輸出為: ```swift Hello world!. Value is 1 Hello world!. Value is 2 Hello world!. Value is 3 ``` 在上面的程序中,要迭代的序列的范圍是 1 到 3。 `i`的值設置為范圍(1)中的第一個數字,并在每次迭代時更新為范圍的下一個數字。 此過程一直持續到到達范圍(3)的末尾。 `For-in`循環執行步驟 | 迭代 | 從范圍(`i`)返回的值 | 輸出 | | --- | --- | --- | | 1 | 1 | `Hello world!. Value is 1` | | 2 | 2 | `Hello world!. Value is 2` | | 3 | 3 | `Hello world!. Value is 3` | * * * ## 在`for-in`循環中舍棄范圍值 如果循環內沒有使用范圍值,則可以在 Swift 中使用`_`(下劃線)將其丟棄,如下所示: ```swift for _ in <range> { <some work here> } ``` ### 示例 2:在`for-in`循環中丟棄范圍值 ```swift // This example neglects value and uses half open range operator for _ in 1..<3 { print("Hello world!") } ``` 運行該程序時,輸出為: ```swift Hello world! Hello world! ``` 在上面的程序中,由于使用了[半開范圍運算符](/swift-programming/ranges#half-open-range-operator "Swift half-open range operator")(`..<`),該序列的迭代范圍是 1 到 2,但不包括下界(1)上限(3)。 `_`忽略范圍(1)中的值,并執行打印語句。 再次調用`print`語句進行下一次迭代,并且過程結束,因為 2 是該范圍內的最后一個值。 沒有范圍值的`for-in`循環執行步驟 | 迭代 | 范圍返回的值 | 輸出 | | --- | --- | --- | | 1 | 舍棄 | **`Hello world!`** | | 2 | 舍棄 | **`Hello world!`** | * * * ## `for-in`循環,使用步幅固定的間隔 如果要使循環在每次迭代中以某個固定值(而不是范圍)遞增,則必須使用[`stride`](https://developer.apple.com/documentation/swift/1641347-stride)方法。 ### 示例 3:使用步幅的`for-in`循環 ```swift let interval = 2 for i in stride(from: 1, to: 10, by: interval) { print(i) } ``` 運行該程序時,輸出為: ```swift 1 3 5 7 9 ``` 在上述程序中,`stride`函數返回數字序列:1、3、5、7、9。 將`i`的值設置為序列(1)的第一個數字,并執行循環內的`print`語句,該語句在控制臺中輸出“1”。 執行該語句后,`i`的值將更新為另一個值(3),并再次調用`print`語句。 該過程一直持續到訪問序列中的所有元素為止。 使用步長的`for-in`循環執行步驟 | 值 | 條件(`i < end`) | `i`(輸出) | | --- | --- | --- | | 1 | `1 < 10`(是) | 1 | | `1 + 2 = 3` | `3 < 10`(是) | 3 | | `1 + 2 * 2 = 5` | `5 < 10`(是) | 5 | | `1 + 3 * 2 = 7` | `7 < 10`(是) | 7 | | `1 + 4 * 2 = 9` | `9 < 10`(是) | 9 | | `1 + 5 * 2 = 11` | `11 < 10`(否) | 停止 | * * * ## 如何使用`for-in`循環訪問集合的元素? 假設您有一個字符串數組,如下所示。 如果您不了解數組,可以將數組視為可以存儲多個值的單個容器。 有關更多詳細說明,請參見 [Swift 數組](/swift-programming/arrays "Swift Arrays")。 ```swift let programmingLanguages = ["Swift", "Java", "Go","JavaScript","Kotlin","Python"] ``` 如果有人告訴您打印所有編程語言怎么辦? 一種方法是使用索引作為`programmingLanguages[0], programmingLanguages[1]` ...來訪問這些元素,依此類推,直到獲得所有元素。 但這太繁瑣了。 這是搶救循環。 您可以使用`for in`循環進行迭代: ### 示例 4:使用`for-in`循環訪問數組(集合)的元素 ```swift let programmingLanguages = ["Swift", "Java", "Go", "JavaScript", "Kotlin", "Python"] for language in programmingLanguages { print(language) } ``` 運行該程序時,輸出為: ```swift Swift Java Go JavaScript Kotlin Python ``` 在上面的程序中,要迭代的序列是一個字符串數組。 `language`的值設置為數組的第一個元素,并執行循環內的`print`語句,該語句在控制臺中輸出`Swift`。 執行該語句后,將使用數組的下一個元素更新`language`,并再次調用`print`語句。 這個過程一直持續到訪問數組的最后一個元素為止。 * * * ### 示例 5:使用 for-in 循環訪問字符串(集合)的元素 由于在 Swift 中[字符串](/swift-programming/characters-strings "Swift strings")也是集合,因此您可以使用`for`循環訪問字符串中的每個字符。 ```swift for value in "I?Swift!" { print(value) } ``` 運行該程序時,輸出為: ```swift I ? S w i f t ! ``` * * * ### 如何使用`for-in`循環訪問集合的索引? 如果要訪問數組的索引(數組中元素的位置,即 0、1、2),則需要使用`enumerated`方法: #### 示例 6:使用`for-in`循環訪問數組的索引 ```swift let programmingLanguages = ["Swift", "Java", "Go", "JavaScript", "Kotlin", "Python"] for (index, language) in programmingLanguages.enumerated() { print("\(index):\(language)") } ``` 運行該程序時,輸出為: ```swift 0:Swift 1:Java 2:Go 3:JavaScript 4:Kotlin 5:Python ``` 在這里,枚舉方法返回數組(`Int`)和值(`String`)組成的元組(`Int`,`String`)。 例如:`(0, Swift)`,`(1, Java)`... 使用`for-in`循環,可以在元組中一對一地訪問這些項目。 如果您不了解元組,則可以簡單地將其視為可以容納不同類型值的容器。 有關更多詳細說明,請參見 *Swift 元組*。 * * * ## 如何在`for-in`循環中使用`where`子句過濾元素? 您也可以使用`for-in`循環和`where`子句過濾內容: ```swift for value in "I?Swift!" where value != "!" { print(value) //removes exclamation sign } ``` 運行該程序時,輸出為: ```swift I ? S w i f t ``` 在上面的程序中,要迭代的序列是字符串(字符集合)。 `value`設置為字符串的第一個字符,并使用`where`條件進行檢查。 如果條件返回`true`,則執行循環內的塊(打印語句),并在控制臺中輸出`"I"`。 執行該語句后,`value`更新為字符串的下一個字符,并再次檢查條件。 如果條件返回`false`,則不執行該塊,并且`value`更新為下一個字符。 此過程一直持續到訪問字符串的最后一個字符為止。 帶有過濾的`for-in`循環執行步驟 | 迭代 | 值 | `value != "!"` | 輸出 | | --- | --- | --- | --- | | 1 | `I` | `true` | `I` | | 2 | `?` | `true` | `?` | | 3 | `S` | `true` | `S` | | 4 | `w` | `true` | `w` | | 5 | `i` | `true` | `i` | | 6 | `f` | `true` | `f` | | 7 | `t` | `true` | `t` | | 8 | `!` | `false` | |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看