WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Day9 - access control - test

Forums > 100 Days of Swift

Can someone clarify this to me please ?

Q4: - I chose false, because there is a private property and no custom init - and the answer was wrong struct Contributor { private var name = "Anonymous" } let paul = Contributor()

Q6: - I chose false, on the same idea as above - and the answer is right struct Doctor { var name: String var location: String private var currentPatient = "No one" } let drJones = Doctor(name: "Esther Jones", location: "Bristol")

What is the difference between these 2?

Also I tried these 2 scenarios in playground and they are both false.


What are the questions? Sorry, it's been a while since I went through this.

At any rate, the difference between these two structs:

The second struct has non-private properties that do not have a default value, so they have to be initialized when the struct is created. The compiler will automatically generate a memberwise initializer for those properties.

The first struct has just one property and it has a default value, so the compiler will not generate a memberwise initializer for it. It's also private, so there wouldn't be a generated init anyway, even if there was no default value. You would get a compile error because there would be no way to initialize the struct.


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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.