Swift: Iterating Over an Array

Iterating Over an Array in Swift:

What if we want to iterate through an array’s items in Swift? A forin loop will be useful!

Following is an example of syntax:

for item in array {
  // Loop body
}

Let’s say we have an Int array that looks something like this:

var numbers = [20, 40, 60, 80, 100];

A forin loop can be written as follows:

for item in numbers {
  print(item);
}

// Prints 20
// Prints 40
// Prints 60
// Prints 80
// Prints 100

This is an extremely useful tool for dealing with and manipulating large amounts of data.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: