Updated for Xcode 14.1
Structs sit at the core of every SwiftUI app, so it’s really important you take some extra time to make sure you understand what they do and how they work.
To check your knowledge, here’s a small task for you: create a struct to store information about a car, including its model, number of seats, and current gear, then add a method to change gears up or down. Have a think about variables and access control: what data should be a variable rather than a constant, and what data should be exposed publicly? Should the gear-changing method validate its input somehow?
As always I’ll write some hints below, but first I’m going to leave some space so you don’t see the hints by accident. As always, it’s a really good idea to try this challenge yourself before looking at the hints – it’s the fastest way to identify parts you feel less confident with.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 6. If you don’t already subscribe, you can start a free trial today.
Still here? Okay, here are some hints:
privateaccess control, you will probably also need to create your own initializer. (Is
privatethe best choice here? Try it for yourself and see what you think!)
mutatingkeyword on methods that change properties!
SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.
Link copied to your pasteboard.