Loop n times in Swift
Table of Contents
There are many ways to loop in Swift. In this post, I will show you how I do when I want to loop for a specific number of times.
How to loop n times with for-in loop
To run a code exactly n
times in Swift, I think for-in
loops with closed range operator (...)
convey the clearest message.
Here is an example of running a code for five times.
for i in 1...5 {
print(i)
}
// 1
// 2
// 3
// 4
// 5
You can achieve the same thing with half-open range (0..<5
) or a different range 0...4
, but I prefer to use 1...5
instead of 0...4
or 0..<5
because it is easier to understand to me since it matched the way we count in real-world.
0...4
matched how we normally count in programming, but it lack the actual number of times that we want to execute,5
.0..<5
contain the number5
, but I think it is more confusing than0...4
.
You can easily support sarunw.com by checking out this sponsor.
Offline Transcription: Fast, privacy-focus way to transcribe audio, video, and podcast files. No data leaves your Mac.
How to loop n times with forEach
You can also use forEach
to achieve the same effect.
(1...5).forEach { i in
print(i)
}
// 1
// 2
// 3
// 4
// 5
You can easily support sarunw.com by checking out this sponsor.
Offline Transcription: Fast, privacy-focus way to transcribe audio, video, and podcast files. No data leaves your Mac.
Make it more Rubyist
In Ruby[1], you can loop for a specific number of times using an Integer
's method, times
.
Here is an example of how we loop five times in Ruby.
5.times do |i|
puts i
end
// 0
// 1
// 2
// 3
// 4
Let's try to do this in Swift just for fun.
extension Int {
func times(_ callback: (Int) -> ()) {
// 1
if self > 0 {
// 2
for i in 0..<self {
callback(i)
}
}
}
}
1 First, we check if the number is more than zero.
2 Then we loop and return the value starting from zero (to make it match the implementation of the Ruby).
Then we can use it like this.
5.times { i in
print(i)
}
// 0
// 1
// 2
// 3
// 4
Normally, I won't introduce trivial extensions like this to the codebase because I think it causes more harm than good.
But this is totally up to your judgment. I just want to show how easy it is for you to extend Swift's capability.
Ruby (programming language) https://en.wikipedia.org/wiki/Ruby_(programming_language) ↩︎
Read more article about Swift, Control Flow, or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareSwiftUI AnyLayout - smooth transitions between layout types
In iOS 16, SwiftUI got a new tool, AnyLayout, that makes it possible to transition between layouts while maintaining the identity of the views.
How to make SwiftUI button with buttonStyle expand to full width
SwiftUI got many beautiful built-in button styles. One problem you might get is it isn't obvious how to control the size of it. Let's learn how to do it.