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.
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.
//For-in loop
var evenNumbers = [Int]()
for number in 0...50 {
guard evenNumbers.count < 5 else {
break
}
guard number % 2 == 0 else {
continue
}
evenNumbers.append(number)
}
print(evenNumbers)
//this will print [0, 2, 4, 6, 8]
-------------------------------------------
//For-each loop
var evenNumbers = [Int]()
(0...50).forEach{ number in
guard evenNumbers.count < 5 else {
break //Error: 'break' is only allowed inside a loop, if, do, or switch
}
guard number % 2 == 0 else {
continue //<e>'continue' is only allowed inside a loop
}
evenNumbers.append(number)
}
print(evenNumbers)
2. Return from scope is not possible from for-in loop
Return from the scope of the for loop is not possible in the case of the for-in loop. Please note that we can use return
in the for-in loop if inside a function, but that will return from the function but not from the for-loop scope.
//For-in loop
var evenNumbers = [Int]()
for number in 0...50 {
guard evenNumbers.count < 5 else {
return //<e>Return invalid outside of a func
}
guard number % 2 == 0 else {
return //<e>Return invalid outside of a func
}
evenNumbers.append(number)
}
print(evenNumbers)
-------------------------------------------
//For-each loop
var evenNumbers = [Int]()
(0...50).forEach{ number in
guard evenNumbers.count < 5 else {
return
}
guard number % 2 == 0 else {
return
}
evenNumbers.append(number)
}
print(evenNumbers)
//this will print [0, 2, 4, 6, 8]
3. For-each can be used with Swift’s closures or first class functions
Since for-each is a function but not an operator, it can be used with closures or first-class functions whereas the same is not possible with the case of the for-in loop.
//For-each loop
func doSomethingWithNumber(for number : Int){
guard number % 2 == 0 else {
return
}
print(number)
}
(0...10).forEach(doSomethingWithNumber)