|
When Paul is demonstrating private(set) in CheckPoint6 solution, he wrote this
Then he said:
So, I tried to understand the concept of we’ve exposed the gear variable to the world, so it’s possible to modify it directly to something impossible like 1000. I tried to take advantage of the "exposed gear variable ", so I wrote Indeed I could make this riculous instance. But even if I add My Question: 1, , if it is NOT a I mean, it is a variable in a specific Struct anyway, it is not a global variable anyway, right ? So how private it could be ? hiding from whom ? 2, Or, if it is a Boat |
|
He means you can't do something like this:
Yes, methods of a struct have full access to the properties of the struct, which is why That's the reason why you can do this without error:
The initializer has full access to |
|
Yes I just tried car.gear with and without private(set) , all clear now. Have a great rest of the day ! Boat. |
|
You’ll hear @twostraws say in his videos that structs get a free member-wise initializer. As @rooster points out, this allows you to create a new Car structure with an insane starting gear. As your Structs become more complex, and as you design structs to be reused in your programs and in libraries you share with other developers, you’ll find business reasons to PREVENT developers from using the default member-wise initializer. This may be an example of that! You’ll never want to create a new car that starts off in gear 1000. But this is exactly what you can do with the default initializer. It’s a bit more work, but probably you’ll want to create your own initializer. Here’s my crack at it:
|
|
I haven't learned about |
|
@boat asks:
I've been corrected by that guy. Don't pay attention to what I said about functions being public by default. Probably my definition of public is also technically wrong, but probably good enough to share with this small group. Also, I don't know what I'm talking about 90% of the time. Paste into Playgrounds:
Try removing the Bonus Code SnipThis was fun. Notice I created a range for the valid gears. It's a But also notice how I validated input to functions. I used the
|
|
Not to be that guy, but... The default access level (with a few edge case exceptions) is actually See Default Access Levels for more details. |
|
I've been corrected by that guy. Don't pay attention to what I said about functions being public by default. Probably my definition of public is also technically wrong, and only passably good enough to share with this small group of learners. Also, I don't know what I'm talking about 90% of the time. Which is about half the time. I have corrected the text. Will review it again, and delete it in total, if still inadequate. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.