Recall & Review
beginner
What is a memberwise initializer in Swift?
A memberwise initializer is an automatically created initializer for structs in Swift. It lets you create an instance by providing values for each property directly.
Click to reveal answer
beginner
Does Swift automatically create a memberwise initializer for classes?
No, Swift does not automatically create a memberwise initializer for classes. It only does this for structs.
Click to reveal answer
beginner
Given this struct:<br>
struct Car {<br> var make: String<br> var year: Int<br>}<br>How do you create a new Car instance using the memberwise initializer?You create it by passing values for each property like this:<br><pre>let myCar = Car(make: "Toyota", year: 2020)</pre>Click to reveal answer
intermediate
What happens if you add a custom initializer to a struct in Swift?
If you add a custom initializer, Swift does not generate the memberwise initializer automatically anymore. You have to write it yourself if you want it.
Click to reveal answer
beginner
Why is the memberwise initializer useful?
It saves time by letting you create instances quickly without writing your own initializer. It also makes your code cleaner and easier to read.
Click to reveal answer
Which Swift type automatically gets a memberwise initializer?
✗ Incorrect
Only structs get an automatic memberwise initializer in Swift.
What do you need to provide when using a memberwise initializer?
✗ Incorrect
You must provide values for all stored properties when using the memberwise initializer.
If you add a custom initializer to a struct, what happens to the memberwise initializer?
✗ Incorrect
Adding a custom initializer removes the automatic memberwise initializer.
Can classes in Swift use memberwise initializers automatically?
✗ Incorrect
Classes do not get automatic memberwise initializers in Swift.
What is the main benefit of a memberwise initializer?
✗ Incorrect
Memberwise initializers let you quickly create instances by providing values for all properties.
Explain what a memberwise initializer is and how it helps when creating struct instances in Swift.
Think about how you create a struct instance without writing your own initializer.
You got /4 concepts.
Describe what happens to the memberwise initializer when you add a custom initializer to a Swift struct.
Consider how Swift handles initializers when you add your own.
You got /3 concepts.