# for...in

[Swift](https://lochiwei.gitbook.io/ios/swift) ⟩ [flow control](https://lochiwei.gitbook.io/ios/swift/flow) ⟩ [loop](https://lochiwei.gitbook.io/ios/swift/flow/loop) ⟩ for...in

{% hint style="info" %}

* ✅ [continue](https://lochiwei.gitbook.io/ios/swift/flow/transfer/continue "mention"): skip to the <mark style="color:red;">**next**</mark> element.
* ✅ [break](https://lochiwei.gitbook.io/ios/swift/flow/transfer/break "mention"): break the iteration <mark style="color:red;">**completely**</mark>. :point\_right: :digit\_one:
* ❌ [return](https://lochiwei.gitbook.io/ios/swift/flow/transfer/return "mention"): <mark style="color:red;">**invalid**</mark> outside of a **function**.
  {% endhint %}

{% tabs %}
{% tab title="📗 參考" %}

* [ ] Advanced Swift, p.&#x20;
* [ ] Sundell ⟩ [Picking between a for loop and forEach](https://www.swiftbysundell.com/tips/picking-between-for-and-for-each/)
* [ ] Sarun ⟩ [How to get index and value from for loop in Swift](https://sarunw.com/posts/swift-for-loop-with-index/)
  {% endtab %}

{% tab title="1" %}
:point\_right: [replit](https://replit.com/@pegasusroe/forin-vs-forEach#main.swift)

```swift
var a1 = [Int]()
var a2 = [Int]()
var a3 = [Int]()
var a4 = [Int]()
var a5 = [Int]()

/*
    for-in loop
    -----------
    - ✅ continue: skip to next element
    - ✅ break   : break the loop completely
    - ❌ return  : invalid outside of a function
*/

// 'break' in for-in loop
for n in 1...10 {
    if n > 3 { break }  // ⭐ break for-loop completely
    a1.append(n) 
}
// ⭐ [1, 2, 3]

print(a1)

/*
    .forEach()
    -----------
    - ⛔ continue: not allowed
    - ⛔ break   : not allowed
    - ❗ return  : return from closure only
*/

// early 'return' in forEach
(1...10).forEach { n in 
    if n > 2 { return }     // ⭐ early return
    a2.append(n)            // ⭐ number NOT appended
}
// ⭐ [1, 2]

// late 'return' in forEach
(1...10).forEach { n in 
    a3.append(n)            // ⭐ number appended
    if n > 2 { return }     // ⭐ late return from closure
}
// ⭐ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// ⛔ 'continue' in forEach not allowed
(1...10).forEach { n in 
    // ⛔ error: 'continue' is only allowed inside a loop
    if n > 2 { continue }     
    a4.append(n)
}

// ⛔ 'break' in forEach not allowed
(1...10).forEach { n in 
    // ⛔ error: 
    //  - unlabeled 'break' is only allowed inside a loop or switch
    //  - labeled 'break' is required to exit an `if` or `do`
    if n > 2 { break }     
    a5.append(n)
}
```

{% endtab %}

{% tab title="2" %}

```swift
// for ... in ... where ...
for article in articles where !article.isDraft {  // ⭐️ where clause
    results.append(article)
    guard results.count < 5 else { break }  // ⭐️ break for-loop entirely
}
```

{% endtab %}

{% tab title="📘 手冊" %}

* Collections ⟩ [Sequence](https://developer.apple.com/documentation/swift/sequence) ⟩ [.forEach(\_:)](https://developer.apple.com/documentation/swift/sequence/3018369-foreach)
* Flow Control ⟩ [for-in loop](https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID121) - iterate over a sequence (array, range, string, dictionary)
  {% endtab %}

{% tab title="👥 相關" %}

* [seq.foreach](https://lochiwei.gitbook.io/ios/swift/collections/sequence/seq.foreach "mention") ⭐️
  {% endtab %}
  {% endtabs %}
