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

SOLVED: day 9 test q6 - access control with private property

Forums > 100 Days of Swift

struct Doctor {
    var name: String
    var location: String
    private var currentPatient = "No one"
}
let drJones = Doctor(name: "Esther Jones", location: "Bristol") //line #

i put true, the answer is false.

answer feedback: Oops – that's not correct. This has a private property, so Swift is unable to generate its memberwise initializer for us.

I dont understand why line # code is wrong. the private property is initialized with string value inside struct already so it does not need to be initialized again in line #, isn't it?

1      

I dont understand why line # code is wrong. the private property is initialized with string value inside struct already so it does not need to be initialized again in line #, isn't it?

A memberwise initializer will still be synthesized for var properties that have a default value. But not for let properties with a default value. This is because a let can't be changed once it has a value but a var can be changed. You can omit a value that has a default when you are initializing, but technically it's still there.

But if that property also has a private access level, then the compilar can't synthesize an initializer and you have to supply it explicitly.

struct Doctor {
    var name: String
    var location: String
    private var currentPatient = "No one"
}
let doc = Doctor(

Will generate the error 'Doctor' initializer is inaccessible due to 'private' protection level

But if you select Doctor in the struct name and use the Generate memberwise initializer feature found under Editor > Refactor, you get this:

struct Doctor {
    internal init(name: String, location: String, currentPatient: String = "No one") {
        self.name = name
        self.location = location
        self.currentPatient = currentPatient
    }

    var name: String
    var location: String
    private var currentPatient = "No one"
}

and now let doc = Doctor( will prompt you with two autocomplete options:

Xcode autocomplete for Doctor struct

2      

thanks! i understand part of ur explanation. next time there is private property, i can either make it 'let' constant type or initializer the properties explicityly.

i couldnt find the Refactor.

"But if you select Doctor in the struct name and use the Generate memberwise initializer feature found under Editor > Refactor, you get this:"

1      

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.