TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Access Control (Day 11)

Forums > 100 Days of SwiftUI

Have a few questions from the test i messed up a bit:

1)

struct FacebookUser {
    private var privatePosts: [String]
    public var publicPosts: [String]
}
let user = FacebookUser()

I chose true (i thought if we have public var so at least we can make public part), but this is false (This has a private property, so Swift is unable to generate its memberwise initializer for us)

2) Otherwise:

struct Contributor {
    private var name = "Anonymous"
}
let paul = Contributor()

I chose false (because have only private part, that didn't want to build in previous example), but this is true

3)

struct School {
    var staffNames: [String]
    private var studentNames: [String]
    init(staff: String...) {
        self.staffNames = staff
        self.studentNames = [String]()
    }
}
let royalHigh = School(staff: "Mrs Hughes")

In this example i chose false, because was afraid of that line with "String...". Could you explain what does it mean "String..." i just remember we used "..." with range of numbers like 1...10, but this one looks different

3      

1) All properties have to be initialized before the struct is ready to be used. Because neither of the properties have default values, they have to be given values in the initializer. But private properties aren't accessible in an init, so you can't supply a value for privatePosts, which means you cannot create a FacebookUser struct. Not only that, but there is no value supplied for the public property, which also means you can't construct a FacebookUser. So this code is not valid Swift.

2) Yes, name is private, but it has a default value, so you don't need to supply one and Contributor() is the correct way to initialize it. So this code is valid Swift.

3) init(staff: String...) utilizes a feature called variadic parameters. Basically, it means an unbounded list of whatever type. So in this case, it means a list of Strings. Could be one, could be three, could be 487. These items are passed into the function as an array, so in this example staff has a value of ["Mrs Hughes"]. So the public property staffNames is initialized with a parameter to the init and the private property studentNames is initialized inside the init by assigning an empty array. This code is valid Swift.

6      

Just to clarify my answer for #1...

Private properties aren't available as part of a compiler-generated memberwise init. You could have an init with a parameter that sets a private property inside the init. But that isn't what the example does, so it's not valid.

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.