101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Learn Swift Step By Step

    Receive aemail containing the next unit.
    • Introduction to Swift
      • 1.1Swift Language Overview
      • 1.2Basic Syntax in Swift
      • 1.3Data Types and Variables
      • 1.4Operators and Control Flow in Swift
    • Classes,Properties and Methods in Swift
      • 2.1Introduction to Classes and Objects
      • 2.2Properties
      • 2.3Methods
      • 2.4Inheritance, Polymorphism & Protocols
    • Collection Types in Swift
      • 3.1Introduction to Arrays, Sets and Dictionaries
      • 3.2Basic Collection Operations
      • 3.3Looping Over Collections & Modifying Collections
      • 3.4Advanced Collection Types
    • Advanced Swift
      • 4.1Error Handling in Swift
      • 4.2Extensions and Protocols
      • 4.3Generics
      • 4.4Concurrency and Multi-Threading in Swift

    Collection Types in Swift

    Looping Over Collections & Modifying Collections in Swift

    In Swift, collections are a fundamental concept that allows us to store and manage groups of related data in a structured way. This article will focus on how to iterate over these collections and modify them.

    Looping Over Arrays

    Arrays are ordered collections of values. To loop over an array in Swift, we use the for-in loop. Here's an example:

    let numbers = [1, 2, 3, 4, 5] for number in numbers { print(number) }

    In this example, number is a constant that takes on each value in the array numbers one by one, and then prints it.

    If you need to know the index of each item as well as its value, you can use the enumerated() method to iterate over the array:

    for (index, number) in numbers.enumerated() { print("Item \(index): \(number)") }

    Looping Over Sets

    Sets are unordered collections of unique values. Like arrays, we can use a for-in loop to iterate over a set:

    let colors = Set(["red", "green", "blue"]) for color in colors { print(color) }

    Since sets are unordered, the order in which the items are printed is not guaranteed.

    Looping Over Dictionaries

    Dictionaries are collections of key-value pairs. When we loop over a dictionary, we get access to each key-value pair as a tuple. Here's an example:

    let capitals = ["France": "Paris", "Spain": "Madrid", "UK": "London"] for (country, capital) in capitals { print("\(capital) is the capital of \(country)") }

    Modifying Collections While Iterating

    In Swift, you cannot directly modify a collection while iterating over it. This is because it could lead to unexpected behavior or runtime errors.

    However, if you need to modify a collection based on its own elements, you can create a copy of the collection and modify that instead. Here's an example:

    var numbers = [1, 2, 3, 4, 5] var squaredNumbers = [Int]() for number in numbers { squaredNumbers.append(number * number) } numbers = squaredNumbers

    In this example, we create a new array squaredNumbers, square each number in the original numbers array, and append it to squaredNumbers. Then we replace the original numbers array with squaredNumbers.

    By the end of this unit, you should have a solid understanding of how to loop over and modify collections in Swift. Practice these concepts with different types of collections and operations to reinforce your learning.

    Test me
    Practical exercise
    Further reading

    Hi, any questions for me?

    Sign in to chat
    Next up: Advanced Collection Types