For-in vs For-each in Swift

For-in and for-each are different variants of for loops in swift which are used to iterate over a range, set or dictionary. Both provide the same functionality but has a few limitations or differences when it comes to conditional access.

This post was originally posted at https://agrawalsuneet.github.io/blogs/for-in-vs-for-each-in-swift/ and reposted on Medium on 03rd Dec 2021.

To understand their differences, let’s try to understand their examples in details first.

For-in loop

For-in loop is used to iterate over a range, set or dictionary using both the indexes as well an element based iteration.

for item in 0...5 {
print(item)
}
let dictionary = ["Suneet": "Engineering", "Ballu": "Sales", "John": "Marketing"]
for (name, department) in dictionary {
print("\(name) is working in \(department) department")
}
let set = ["Suneet", "Agrawal", "Ballu"]
for item in set {
print(item)
}

For-each loop

For-each loop can also be used to iterate over a range, set or dictionary using both the indexes as well an element based iteration.

(0...5).forEach{ item in
print(item)
}
let dictionary = ["Suneet": "Engineering", "Ballu": "Sales", "John": "Marketing"]
dictionary.forEach{ name, department in
print("\(name) is working in \(department) department")
}
let set = ["Suneet", "Agrawal", "Ballu"]
set.forEach{ item in
print(item)
}

Difference between for-in and for-each

1. Break and continue statements cannot be used in for-each loop

break and continue are the basic syntaxes of for loop which is used to either break the loop iteration or to continue to the next element.
Since for-each is not an operator but it’s a function, break and continue can’t be used inside a for-each iteration.

Please continue reading at https://agrawalsuneet.github.io/blogs/for-in-vs-for-each-in-swift/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Flutter iOS Dark Theme Status Bar Color

Applying Auto-Layout to a UIScrollView in Interface Builder

Interface Builder’s Document Outline

Time Steering in Swift 5.5

Capacitor/Cordova (and Ionic) Apps are Native Apps

What is the ~= Operator in Swift?

Class Vs Struct with Example in Swift

How to start using iOS Deep Link

Empty field validations swiftUI

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

@State Property Wrapper Explained

MVVM in SwiftUI

Variable Definition, Arrays, Dictionaries and Set Structures in Swift

SwiftUI View to UIImage