Created
November 18, 2022 09:35
-
-
Save EvolvingParty/2eb54522ddfe03ad290d90dcdeb10355 to your computer and use it in GitHub Desktop.
Day 11 100 Days of SwiftUI – Hacking with Swift. Checkpoint 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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