LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Checkpoint 6: Validate my solution

Forums > 100 Days of SwiftUI

Hello! I would appreciate some assistance in verifying whether my solution for checkpoint 6 is accurate. Your help is greatly appreciated. Thank you!

Here's my code

// Create a struct to store information about a car. Include the following:
//      Current Model
//      Number of Seats
//      Current Gear
// Add a method to change gears up or down
// Think about variables and access control
// Don't allow invalid gears - 1-10 seems like a fair maximum range
struct Car {
    let model: String
    let numberOfSeats: Int
    private(set) var currentGear: Int

    mutating func shiftGearUp() -> Bool {
        if (currentGear >= 1 && currentGear < 9) {
            currentGear += 1
            return true
        }
        return false
    }

    mutating func shiftGearDown() -> Bool {
        if currentGear <= 10 && currentGear > 1 {
            currentGear -= 1
            return true
        }
        return false
    }
}

var firstCar = Car(model: "Honda Civic", numberOfSeats: 5, currentGear: 3)
firstCar.shiftGearUp()

I'd also like to ask if my usage of private(set) is correct. It's still quite unclear to me and honestly, I just guessed it. Any more information that would provide further clarity would be greatly appreciated.

2      

Look good. private(set) will let you set (initialize) but will not let you change that property ontside the struct

This can not be done (Error: Left side of mutating operator isn't mutable: 'currentGear' setter is inaccessible)

firstCar.currentGear += 1

so to change currentGear have to use the methods shiftGearUp() or shiftGearDown() inside the struct

The only thing is why do you need to return a Bool from the methods!

mutating func shiftGearUp() {
    if (currentGear >= 1 && currentGear < 9) {
        currentGear += 1
    }
}

mutating func shiftGearDown() {
    if currentGear <= 10 && currentGear > 1 {
        currentGear -= 1
    }
}

Whole code

struct Car {
    let model: String
    let numberOfSeats: Int
    private(set) var currentGear: Int

    mutating func shiftGearUp() {
        if (currentGear >= 1 && currentGear < 9) {
            currentGear += 1
        }
    }

    mutating func shiftGearDown() {
        if currentGear <= 10 && currentGear > 1 {
            currentGear -= 1
        }
    }
}

var firstCar = Car(model: "Honda Civic", numberOfSeats: 5, currentGear: 3)
firstCar.shiftGearUp()
print(firstCar)

2      

I apologize if this might sound a bit stupid but why is it that we don't need to return a bool? I thought we have to return a bool so that it knows if we can shift our gears either up or down (Bool serves as the validation of the gear range). Am I misunderstanding things incorrectly here? Feel free to correct me as I really want to be able to grasp why.

2      

Well, for one thing, you return a Bool, but you don't actually capture that information anywhere.

firstCar.shiftGearUp()

What are you doing with the Bool result? Nothing.

Returning anything is not necessary. You can do it if you want, but then you should do something with that result or otherwise what's the point of returning anything at all?

2      

I see. So that means I only have to return something if I have to use that returned value somewhere? Otherwise, if I don't need whatever's being returned by our function, then I can completely remove any -> data type statments?

2      

A new hacker may be forgetting who's in charge when writing code!

So that means I only have to return something if I have to use that returned value somewhere?

You're in charge of the logic!

Don't forget who's writing the code! You!

You may have a business use case where you're telling your car, the Hacker900, that it needs to switch down one gear. But your use case says to switch down on demand. So you might code the following:

mutating func shiftGearDown()  {
    if currentGear <= 10 && currentGear > 1 {
        currentGear -= 1 // <-- going down one gear.
    }
}

But your quality control team might wonder, did we really shift down? Can we verify our request was executed? In this business case, you might write the following code:

mutating func shiftGearDown() -> Bool {
    if currentGear <= 10 && currentGear > 1 {
        currentGear -= 1
        return true // <-- Returns TRUE if the gear was shifted down.
    }
    return false // <-- Otherwise there was some failure.
}

if shiftGearDown() {  // <-- shiftGearDown() will attempt some work, then return true or false, if completed.
    print("Successful gear shift! Well done!")
} else {
    print("Gear shift failed. Probably a PEBCAK error.")  // <-- Report the failure
}

Whether you include a return bool or not is up to you and your business case. You're the programmer!

Keep coding!

2      

So that means I only have to return something if I have to use that returned value somewhere?

As @roosterboy said you not using the Bool value anywhere, however if you did this while loop

while firstCar.shiftGearUp() {
    print(firstCar.currentGear)
}

It would keep add one to currentGear if can is able to, then exit loop when it can't and now you are using the Bool value.

In your first code I could not see you used Bool anywhere and the method was just changing the currentGear value so sugeested to remove the return value.

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot 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.