100 Days of SwiftUI

Day 11

2026-04-28

Structs, part 2

How to limit access to internal data using access control

  • private says the properties/methods of a func can only be used by that func
  • fileprivate “don’t let anything outside the current file use this”
  • public explicitly open to all comers
  • private(set) anyone can read it, but writing is reserved for internal methods
  • if you use private you likely have to create your own initializer he notes this in passing at the end, but the quiz is full of them!

Static properties and methods

  • use cases: example data, grouping constants that are used in multiple places
  • static keyword in front of var and/or func
  • this means that the var and/or func are properties of the struct and not the instance (does this mean that the struct doesn’t make instances?)
  • cannot access static properties from a non-static struct
  • however if you are in a non-static struct you can read static struct properties
  • ugh self is the current value of a struct but Self is the current type of struct and now I’m 😠

Quizzes

  • Access control — 8/12
  • Static properties and methods — 9/12

Checkpoint 6

Create a struct to store information about a car, including its model, number of seats, and current gear, then add a method to change gears up or down.

These are really stretching my brain!

struct Car {
    var model = "My Car"
    var seats = 0
    private var gear = 0
    var dirUp = true
    let gearRange = 1...6

    init(
        model: String = "My Car",
        seats: Int = 0,
        gear: Int = 0,
        dirUp: Bool = true
    ) {
        self.model = model
        self.seats = seats
        self.gear = gear
        self.dirUp = dirUp
    }

    mutating func changeGear(dirUp: Bool) {
        if dirUp && gearRange.contains(gear + 1) {
            gear += 1
            print("You are in \(gear) gear")
        } else if !dirUp && gearRange.contains(gear) {
            gear -= 1
            print("You are in \(gear) gear")
        } else {
            print("You're at the boundaries of life!")
        }
    }
}