|
On question 1, we are presented with this INVALID code.
It is invalid because 'private var privatePosts' cannot be generated by the memberwise initializer. "This has a private property, so Swift is unable to generate its memberwise initializer for us." HOWEVER, on question 4, we are presented with this VALID code:
How can this be valid? 'name' is a private var and thus cannot be generated by the memberwise initializer. They are virtually identical in principle yet one is valid and the other is invalid. What is going on? |
|
Dan thinks the Swift compiler is messing with him!
In both cases, you are asking the compiler to set aside portions of memory for Swift objects that hold named variables, and possibly methods. Direct AssignmentBy definition, the
The compiler can create the Incomplete AssignmentBy definition, the Because it's marked But how?
Keep coding! |
|
@Obelix, thank you for your great explanation. However, I have an issue that seems to contradict your explanation. This is question 6 of that same quiz:
This code is marked as INVALID. The solution states "Oops – that's not correct. This has a private property, so Swift is unable to generate its memberwise initializer for us." However, we see that "No one" has been assigned to private var currentPatient, which should work per your explanation. Can you please clarify? |
|
Dan gains points for persistence! Nice.
Too many times I've found I'm incomplete, or just inaccurate. Sorry! I reordered my examples above in the Incomplete Assignment section to point out that the compiler, indeed, would complain. PlaygroundsI hope you are using XCode's Playgrounds? This is a great advertisement to any new developer why Playgrounds is an awesome tool to use in your development journey. I encourage you to fire it up and create a few test structs, both with and without private vars. Create them. Destroy them. Try out numerous combinations. By building, breaking and fixing your own examples, you'll gain a deeper understanding than @twoStraw's videos and my one forum post. Here are a few structs to get you started in Playgrounds:
It's all about the initializer. You have a plan for a Without writing your own initializer code, you usually can create a new
In this case however, the Here are two more examples for your Playgrounds. I encourage you to make additional tests. Build and break.
And to verify the compiler's intentions. Toss this into the Playgrounds:
|
|
|
|
Joshua would like additional clarification:
I hope you tried Playgrounds? Have you tried creating several structs in Playgrounds to see what works and what doesn't work? It would be great to know you're trying this out on your own and experimenting. In short, the In Question 6, the You will not get this for free:
It would be nice, because to create a So you either need to initialize each variable inside the struct. This is a custom initializer:
The code in Question 6 does not contain a custom initializer. Therefore the code in Question 6 is invalid. |
|
Thank for the help, @Obelix. I'll play around in the playground to run the code and experiment with the concepts. Much appreciated! |
|
I think I got it.... (after time messing in playgrounds) A. If you use public variables you can use memberwise init provided by swift (or custom if you choose) B. If you have private variables in struct you have to do both of the following:
Does that sound right? Thanks in advance . |
SPONSORED Transform your career with the iOS Lead Essentials. This Black Friday, unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a free crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.