When I use looping in Swift

Photo by Srinivas JD on Unsplash

List of loops I use from frequently used to new I know what they are used for. A few more loops I forgot, this is the list I remember.

1. Standard (For — In Loop)
Such as items in an array, ranges of numbers, or characters in a string.
this example for — in loop

2. Loop with key (For — In Loop)
Same as standard loop but this loop uses key as index. an array use enumerated and this it work :

3. Loop over range (For — In Loop)

Make range use index, set from first index to last index between index you add three dots. Here’s that code example with the closed range operator.

4. Loop For Each

This loop same like normal for — in loop, but this use for each item can’t add optional items. and then check example below:

5. Loop with Zip (For — In Loop )

For example, both two array. you can use zip loop. If one of array data shorter than the others, the data that is looped from the data that is less in number. this example use data model combine two data array and put to model.

6. Loop Key and Zip (for In Loop)

This loop use key and zip, this often helps me in the condition that two data are processed at the same time by requiring a key.

--

--

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