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

SOLVED: access control with private property

Forums > 100 Days of Swift

Why is this valid?:

struct Doctor {
    private var currentPatient = "No one"
}

let drJones = Doctor()

but not this:

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

let drJones = Doctor(name: "Esther Jones", location: "Bristol")

From the first case I understand that if a private variable has a default value and doesn't need to be in the initializer, then you can create an instance. But in the second case if you fill in the variables and ignore the private variable it sends you an error: 'Doctor' initializer is inaccessible due to 'private' protection level

3      

I'm not sure of the reason, but setting a default value this way works...

struct Doctor {
    var name: String
    var location: String
    private var currentPatient: String

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

4      

The reason your first example works can be found in the documentation:

Swift provides a default initializer for any structure or class that provides default values for all of its properties and doesn’t provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.

So, since the Doctor struct in your first example provides a default value for its only property, the compiler can generate the default initializer Doctor() that simply creates a Doctor with currentPatient set to "No one".

The reason your second example doesn't work can also be found in the documentation:

The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private.

So, since you a) have a private stored property in your struct, and b) are relying on the default memberwise initializer synthesized for you by the compiler, you get the error message 'Doctor' initializer is inaccessible due to 'private' protection level

@FlyOstrich's solution works because it uses an explicitly written initializer rather than the default one synthesized by the compiler..

5      

Thank you very much now I understand.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.