UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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?

3      

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

4      

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:"

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.