Loops: Free iOS Development Tutorial

Explore the basics of iOS Development, focusing on the use of while loops, for loops, and for-in loops, with step by step instructions on setting up and utilizing these essential programming tools in Xcode.

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Note: These materials are provided to give prospective students a sense of how we structure our class exercises and supplementary materials. During the course, you will get access to the accompanying class files, live instructor demonstrations, and hands-on instruction.

Topics covered in this iOS Development tutorial:

While loops, For loops, For-in loops

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Exercise Overview

Loops are an incredibly important and often-used element in your programming tool belt. In this exercise, you’ll learn how to properly set up and use while loops and for-in loops.

Getting Started

  1. Launch Xcode if it isn’t already open.

  2. Go to File > New > Playground.

  3. Under iOS, double–click on Blank.

  4. Navigate into Desktop > Class Files > yourname-iOS App Dev 1 Class.

  5. Save the file as: Loops.playground

  6. Click Create.

While Loops

While loops run through the code inside the loop until the condition is false. You would use this type of loop when you don’t know beforehand how many iterations the loop should go through.

  1. Start creating a while loop by typing the bold code, replacing the var str line:

    import UIKit
    
    while
    
  2. Add a condition to the while loop, telling it to execute the code between the curly braces while the counter is less than 2:

    while counter < 2 {
    
    }
    
  3. Notice on the left of the while line, there is a red alert red alert icon. Click it to see the error on the right: Use of unresolved identifier ‘counter'

    We are getting this alert because we are referencing a variable, counter, which hasn’t been declared yet.

  4. Declare the counter variable as shown in bold:

    import UIKit
    
    var counter = 0
    while counter < 2 {
    
    }
    

    The alert should disappear.

  5. Type the bold code:

    var counter = 0
    while counter < 2 {
       print("\(counter)")
    }
    

    Notice on the right, there’s an ever-increasing number. You’ve just created an infinite loop! Because we specified counter is equal to 0 and that the loop should run while counter is less than 2 (which 0 will always be) it will run forever. While it’s kind of a cool effect, infinite loops are generally not very practical and could crash your app.

  6. Instead, let’s increment the data so that the counter number increases each time the loop runs. Replace the print function as shown:

    var counter = 0
    while counter < 2 {
       counter = counter + 1
    }
    

    This increases the counter by 1 every time the loop runs.

  7. Print the value with the following code:

    while counter < 2 {
       print("\(counter)")
       counter = counter + 1
    }
    

    On the right, it prints (2 times). The loop stops there because we set it to only run as long as the counter is less than 2–once it’s false, the loop exits.

For-In Loops

For-In statements are used to loop through the properties of an object. In this instance, you’ll loop through the items in an array.

  1. Loops are very useful for iterating over collection types such as arrays. Let’s create an array using our players from the last exercise. At the bottom of the Playground file, declare a new array:

    var players = ["John", "Laurie", "Omar", "Holly", "Sam"]
    

    We’ll use a for-in loop to iterate over this array. Let’s say you want to create a display in a table view. This is a common practice (for example, the list of emails in a mail app is typically displayed this way). When a loop runs, it pulls each index from the array and populates a table with that data.

  2. At the bottom of the Playground file, type:

    for name in players {
    
    }
    

    This loops through the indexes in the players array and assigns each item to the name variable. For each time it loops through, it prints the name.

  3. Add a print function:

    for name in players {
       print("\(name)")
    }
    
  4. Mouse over the right side of the newest (5 times) line. When you see the Quick Look eye quick look eye, click it.

  5. A pop-up will appear. If you only see one name listed in it, Right–click on the pop-up and choose Value History. Here is our list of players!

  6. Now you’ll look at using for-in loops to iterate over dictionaries. First, create a dictionary. At the bottom of the file, create a dictionary containing airport code abbreviations and their respective cities:

    var airports = ["JFK": "New York", "ATL": "Atlanta", "DEN": "Denver"]
    
  7. Create a for-in loop for airports:

    var airports = ["JFK": "New York", "ATL": "Atlanta", "DEN": "Denver"]
    
    for (airportCode, city) in airports {
    
    }
    

    airportCode is the variable name that represents the key. city is the variable name that represents the value. They are separated by a comma. airports is the dictionary name.

    You can break this down as: for (key, value) in dictionary { }

  8. Add a print function:

    for (airportCode, city) in airports {
       print("The code is \(airportCode) for the city of \(city)")
    }
    
  9. Mouse over the right side of the (3 times) line. When you see the Quick Look eye quick look eye, click it.

  10. A pop-up will appear. If you only see one name listed in it, Right–click on the pop-up and choose Value History. The pop-up should list:

    • The code is ATL for the city of Atlanta
    • The code is DEN for the city of Denver
    • The code is JFK for the city of New York

    As you can see, the for-in loop will iterate over a dictionary, automatically pulling out the key and value.

  11. Save and close the file. Keep Xcode open, as we’ll use it in the next exercise.

How to Learn iOS & Web Development

Master iOS development, web development, coding, and more with hands-on training. iOS development involves designing apps for Apple mobile devices with tools like Xcode and SwiftUI.

Yelp Facebook LinkedIn YouTube Twitter Instagram