5-Day Swift Coding Challenge: Day 5

Nov 21 2025 · Swift 6.0, iOS 18, Swift Playground 4.6

Lesson 01: Day 5: Making an App

Demo: Looping in SwiftUI

Episode complete

Play next episode

Next
Transcript

To get started, open the project in progress. This should be the state of the maze app after the last homework. At this point, we want to add all the emoji’s into an array. That way, we can store our data, update it on the fly, and even add new mazes. Update it to the following:

  var data = [
  ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🐰","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩"],
  ["🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟩"],
  ["🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩"],
  ["🟩","🟧","🟧","🟧","🟧","🟧","🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟩"],
  ["🟩","🟧","🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩"],
  ["🟩","🟩","🟩","🟩","🟩","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟧","🟩"],
  ["🟩","🟧","🟧","🟧","🟧","🟧","🟩","🟩","🟩","🟩","🟧","🟩","🟧","🟩","🟧","🟩"],
  ["🟩","🟧","🟩","🟩","🟩","🟧","🟩","🟧","🟧","🟧","🟧","🟩","🟧","🟩","🟧","🟩"],
  ["🟩","🟧","🟧","🟧","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟩"],
  ["🟩","🟩","🟩","🟧","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟧","🟧","🟧","🟧","🟩"],
  ["🟩","🟩","🟩","🥕","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩","🟩"],
]

This is the entire maze, now contained inside of a multi-dimensional array. This is contained within the data array. Of course, the next step is to actually update the code to use this array.

Delete the previous Text views that hold the existing maze. Update it to the following:

ForEach(0..<data.count, id: \.self) { rowIndex in
    HStack {

    }
}

This now loops through each element in the array. Remember, each row contains another array containing all that information. The row index keeps track of the current row.

Now to print out the row. Add the following:

Text(data[rowIndex].joined(separator: ""))

There is a lot of stuff going on here. Let’s break it down. First, we get the current row based on the current row index. Remember, this fetches the contained array. So, at the zero index, we get the very first array.

Next, we call the joined method. This takes all the elements in an array and combines them into a single string. We then pass that it to the Text which prints out the variables.

While this works, it’s not enough. You need to update the player as they move. For this, you need to add your own functions.

See forum comments
Cinema mode Download course materials from Github
Previous: Working with Multidimensional Arrays Next: Understanding Functions