100 Days of SwiftUI
2026-04-28
Structs, part 2
How to limit access to internal data using access control
privatesays the properties/methods of afunccan only be used by thatfuncfileprivate“don’t let anything outside the current file use this”publicexplicitly open to all comersprivate(set)anyone can read it, but writing is reserved for internal methods- if you use
privateyou 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
statickeyword in front ofvarand/orfunc- this means that the
varand/orfuncare properties of thestructand not the instance (does this mean that thestructdoesn’t make instances?) - cannot access static properties from a non-static
struct - however if you are in a non-static
structyou can read staticstructproperties - ugh
selfis the current value of astructbutSelfis the current type ofstructand 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!
PuzzleMy solution
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!")
}
}
}