NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Checkpoint 6 Review

Forums > 100 Days of SwiftUI

Hello world,

Below is a solution to checkpoint 6 i've drafted. The solution works, but i'd like to know if there's anything I could correct or improve. The idea here was to randomize the current gear between the two constraints listed; one and ten, as noted in the challenge. Then have the gears change respective of the amount of gears given. In this example from my testing, I get 4 (randomized), then 8, then 4 again.

Thanks.

struct Automobile {
    let model: String
    let seats: Int
    private var currentGear: Int

    init(model: String, seats: Int) {
        self.model = model
        self.seats = seats
        self.currentGear = Int.random(in: 1...10)
        print("Current gear is \(currentGear)")
    }

    mutating func gearUp(gears: Int) {
        if currentGear <= gears {
            currentGear += gears
            print("Current gear after shifting up is \(currentGear)")
        }
    }

    mutating func gearDown(gears: Int) {
        if currentGear >= gears {
            currentGear -= gears
            print("Current gear after shifting down is \(currentGear)")
        }
    }
}

var car = Automobile(model: "BMW", seats: 4)
car.gearUp(gears: 4)
car.gearDown(gears: 4)

   

Your code works well, however you can add some errors with explanations when shifting gears fails. for example if the current gear is 5 and the user requests to add 6 to it you could print that the gears would be out of bounds if the user did that. same with gearDown(). Then you can check the model string and if it is equal to something specific(bmw for example) then the max gear numer would be X else if its something else(audi for example) then the max gear would be Y, and if those 2(or however many you might implement) return false then have a default number of gears. Lastly you should try yourself to make some challenges for yourself, creativity plays a BIG role in coding, think of it as art.

if you're stuck on any of these reply to me and i'll send you the solution(or a hint to whatever problem you may be facing). -Alex

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.