I'm not quite sure what you mean by "I can access the first struct's property but I can't with the second." In the example you give, you aren't accessing any properties from either of the structs.
I think what you are trying to say is that you can create a Contributor
but not a FacebookUser
even though both have private
properties.
You can create a Contributor
because name
has a default value so you don't need to supply one in an initializer. Swift is unable to create a memberwise initializer for you because of the private
property, but you don't need one here since there are no other properties. So you can just create a Contributor
using the bare init()
initializer.
You can't create a FacebookUser
in this example for 2 reasons:
-
You cannot assign a value to privatePosts
due to its private
access level. Swift is unable to automatically generate a memberwise initializer for you because of that and you have no other explicit initializer in which you can assign a value to privatePosts
.
-
You don't assign a value to publicPosts
To get this code to work, you would need to add something like this:
init(privatePosts: [String], publicPosts: [String]) {
self.privatePosts = privatePosts
self.publicPosts = publicPosts
}
(There are other ways you could construct the struct and its initializer to accomplish the same thing. This is just one example.)