|
Enjoying the updated videos! For Checkpoint 6 Paul challenges us to write a Car struct. Here's my attempt. Try it on your own before looking at my solution.
I tried to add validation code as a property observer. In the willSet clause you get a free variable newValue. I checked to see if newValue was valid ( 1 to 10). If it was outside the range, I wasn't sure how to change it. That must come in a later lesson? BONUS QUESTION: What does the playground compiler do if you add another option to the GearDirection enum? Try adding reverse to the enum. What does the compiler do? See Enumerations for more details.
|
|
You would do it in
|
|
Here is my take on solving this:
|
|
I came up with this solution. Not sure if this is an overkill to solve it like this. My goal was to minimize effort on code change when a cars would get more, less or other gears.
|
|
Daniel posts a clever implementation and states:
Well done! Indeed, this is overkill. But only in the sense that this was a simple homework exercise! On the otherhand, your solution shows a great understanding of the 'keep it simple' principles and a forward looking vision of code reuse. These are hard skills to teach, and you're demonstrating them early. Nice! Keep posting, it will be nice to follow your progress. Also, since you're demonstrating advanced techniques, please jump in and answer other's questions! This will help in a few ways. First, and foremost, if you are trying to solve other coder's SwiftUI problems, you'll gain a better understanding of the concepts which will help you solidify your understanding. Second, this is a site dedicated to helping folks all over the world build their skills. This site thrives on contributions from all sorts. Please contribute. Third, it's great fun! |
|
Looking at the solutions above, worried my approach might be overly simplistic but thought I'd share just in case. This is for a manual transmission. What do you think?
|
|
Mario is worried:
You get full points for meeting the requirements for this challenge! Mind you, I would not drive a car using your software!! The goal isn't to implement a Tesla-level transmission solution. You just needed to write a Keep coding! And ask more questions! |
|
Well, after running through Day 9 three times and still feeling like I only took in about 10% of it, I pushed on into Days 10 and 11 and finally made it to Checkpoint 6. I'd love some feedback as to whether I've covered all of what was required here. In any case, it was good fun and a welcome break from those pesky closures.
|
|
Ben completed Day 10 and 11 and asks for feedback:
Nice! Who doesn't want to take a ride in a 'stang? Don't worry so much about trying to cover 'all of what was required'. You're getting a lot of new info. And your code shows you are practicing. That's the important part. Practice writing harsh, apprentice level code. Get the code to compile and work. Working code = success! Next, teach yourself to review other people's code. Try the Rubber Duck 🐤 technique to explain SwiftUI code to yourself. As you progress, you'll unlearn some techniques and get yourself on track to write fluid, Swifty code. Feedback
This is a snip from your code. See how you commanded this struct to have a maximum of 10 gears? Then you defined text labels for each of the gear. You're on Day 11? This is exactly the code you should be writing! ✔︎ However, this might not make the grade in a team environment. Program managers call this a maintenance nightmare. Why? Because you now have the distinct possibility that the hard coded So, instead, what might you do? Computed PropertiesTake a look at @twoStraws' article on computed properties. See -> Computed Properties In your case, you have a
Keep coding! |
|
Thanks so much for the feedback, @Obelix! That's great advice about the computed property for maxGear. My plan is to revisit each checkpoint at the end of the consolidation points to see where I can trim the fat and also apply any additional knowledge retrospectively. |
|
Hello! Here is my code for this challenge. I really like the solution I came up with, as it uses only one method. However, any opinions on how to make it more simplistic and elegant are welcome. I'm sure there's room for improvement. Thoughts?
This would print, "Gear 2 is now changing to 3" at the bottom of the screen. Thanks! :) |
|
Hi! I'm the kind of dev that only does what is required (a lazy one that is). The best code is the one that wasn't written at all. Here is my take at checkpoint 6. Thoughts?
|
|
Alphonso shares clever code and asks for feedback!
Keep coding! |
|
I love that there are so many ways to tackle a problem with code :-) Here is my implementation:
Then I can make the calls below :
and receive the following feedback:
|
|
Stuart joins in sharing his take:
ObservationsOne observation I have for your solution... what gear am I currently in?
Also, this code looks a lot like Java, or C++
You might look through other answers here for a more Swifty approach. In particular, Swift provides some convenience methods for arrays. Here's a more Swifty way to get the first and last element of an array.
Keep Coding! |
|
Thank you, Obelix. I see you are a very active member on the forum. I appreciate all the time and effort you put in to help us! |
|
hi all. i thought id share my code and wouldnt mind some feedback please. here in the uk i'm used to gears where i choose the number rather than shift up or down. so thats what i went with! have i taken the task too simply? thanks
|
|
Hi everyone, Here is my solution to Checkpoint 6 and I welcome any feeback you may please have for me. Thanks in advance! :)
|
|
Coming from germany where I learned driving with a "manual" gear shift back in the days I also used an approach that lets you type the value instead going just "up", "down" etc. Going from 10 to 1 immediately might feel a little strange though ;-) What do you think?
This will result in Model: Mercedes Seats: 5 Gear: 10 |
|
Here's my attempt. Any feedback welcome. I saw that some people used private(set) and enum. I didn't think of those and went another way with a bunch of if statements. Thanks for everything!
|
|
Here is my solution to checkpoint-6. If any suggestions or feedback to give on this , you are welcomed ! Happy coding !
|
|
Helo everybody This is my first interaction here Can you check my solution for checkpoint 6 :
Car(carmodel: "Citroen", numberOfSeats: 5, currentGear: 5) Gear is now 5 Gear is now 4 Gear is now 3 Gear is now 2 Gear is now 1 |
|
Hello good people! This is my code, but I'm not 100% sure if I did it correct (I always have that feeling after completing a challenge and especially if I see that Paul's solution is different ). Any thoughts/feedback?
|
|
Tried to incorporate as much as I learned so far. There's much room for improvement I am sure. The do - try - catch bothers me a bit. Isn't there a more elegant solution?
|
|
Hello all, I thought Ill add my solution on checkpoint 6 as well. You're welcomed to give any feedback on this. Thank you and happy coding!
|
|
https://www.hackingwithswift.com/forums/100-days-of-swiftui/checkpoint-6-changing-gears/10129 I done the Checkpoint 6 by myself but my code is ugly. Then I come here and reworked the code example from beginning of the post:
|
|
@Amir add yet another solution to this puzzle! Nice job! If you were on my development team, we might ask you to take another look at your solution to answer this question: It is safe to switch from 4th gear directly to reverse gear? How might you update your code to test this condition? Also, can you share why the currentGear variable is private? There's a good reason. Why do you think it's private? Keep Coding! |
|
I have a simple solution as well, just wanted to start interacting
|
|
@Obelix
Hi! Sorry I had a long pause in learning because of some circumstances. Answer to first question is: "It is not safe to switch from 4th gear to reverse, because it can break gearbox, so if it is an electronic switcher with touch control i will implement a step by step shifting to avoid this. But if we have other control system it will be error condition with warning like: 'This action can totally break your gearshift, please make shifts consistent one by one down to neutral before doing reverse'." Here an updated code with answer to the second question:
And to the 3rd question I can answer: Because of the private variable parameter can guard the variable from unnecessary changes outside of struct's function, it is absolutely important for states like the account balance or something like this. Thank 'u for your attention to my soltion, sorry for my poor English. |
|
I am not sure why most solution, make an assumption that we can only change 1 gear at a time. I have try to drive from Neutural to 2 or even 4 gear before. Also, if we limit the input, I don't think we need to validate it? or am I missing something?
|
|
Been coding for alot of years but still fairly new to Swift & SwiftUI. I think I coverd all the bases and allow for other number of gears instead of default 10 if one wants to. I do my checks in the Switch statement since it is one simple check. Then default and return if they try an invalid shift. Use private(set) on curGear so only mutating func can change it.
Output
|
|
@ScottThePilot brings a ship safely into port!
Very clever use combining the You'll be answering forum questions in no time! |
|
Thank you sir... I doubt I will be answering things but who knows... I thought "where" was sort of the easy and efficeint way to add that one little check to an already efficient switch statement. Swift really is pretty cool language with lots of hidden gems like that... |
|
I've just started to learn how to code but I went into this trying to make the code as short as possible for example I saw many people us currentGear.up or something along those lines which I felt would be rather annoying if you wanted to switch lets say 2 or more gears at once, anyways heres my code let me know what you guys think!
|
|
How about my approach? I think it does cover all the points mentioned in the challenge. Too straight to the point?
|
|
I worked hard and made new solution for this challenge
Be free to use this solution for improving your code |
|
Nothing new here, but just another example of a newbie:
I don't want the driver to skips gears. :) I like the use of enum in previous examples and putting the shift up and down code into one func. Putting in neutral and reverse is also a good idea. Though my Corolla has a CVT so this gear thing is foreign concept. :) Also with EVs. |
|
Here is my solution to this:
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.