Skip to content

Instantly share code, notes, and snippets.

@EvolvingParty
Created November 18, 2022 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvolvingParty/2eb54522ddfe03ad290d90dcdeb10355 to your computer and use it in GitHub Desktop.
Save EvolvingParty/2eb54522ddfe03ad290d90dcdeb10355 to your computer and use it in GitHub Desktop.
Day 11 100 Days of SwiftUI – Hacking with Swift. Checkpoint 6
import Cocoa
///Create a struct to store information about a car
///Include its model, number of seats, and current gear.
struct Car {
let model: String
let numberOfSeats: Int
private(set) var currentGear: Int {
willSet {
print("\(model) gear was \(currentGear)")
}
didSet {
print("\(model) current gear is \(currentGear)")
}
}
mutating func increaseGear(by: Int) {
if currentGear + by > 10 {
print("The car dosent have that many gears")
} else {
currentGear += by
}
print("\(model) current gear is \(currentGear)")
}
mutating func decreaseGear(by: Int) {
if currentGear - by <= 0 {
print("Cant go below 0")
} else {
currentGear -= by
}
print("\(model) current gear is \(currentGear)")
}
}
var car1 = Car(model: "Honda HR-V", numberOfSeats: 5, currentGear: 5) //WORKS
//car1.model = "Test" !Cannot assign to property: 'model' is a 'let' constant
//car1.currentGear += 2 !Left side of mutating operator isn't mutable: 'currentGear' setter is inaccessible due to private(set). Can only read value
car1.increaseGear(by: 1) //WORKS!
car1.decreaseGear(by: 1)
car1.decreaseGear(by: 3)
car1.decreaseGear(by: 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment