Recall & Review
beginner Click to reveal answer
What does the
CaseIterable protocol do in Swift enums?It allows an enum to provide a collection of all its cases, so you can easily loop through them.
beginner Click to reveal answer
How do you make an enum conform to
CaseIterable?Add
CaseIterable after the enum name, like enum Direction: CaseIterable { ... }.beginner Click to reveal answer
How can you access all cases of a
CaseIterable enum?Use the static property
allCases, for example: Direction.allCases returns a collection of all cases.intermediate Click to reveal answer
What type is
allCases in a CaseIterable enum?It is a collection (usually an array) of all enum cases, with type
[EnumName].beginner Click to reveal answer
Why is
CaseIterable useful when working with enums?It lets you easily loop over all enum cases without manually listing them, which saves time and reduces errors.
How do you declare an enum named
Color that supports iterating all its cases?✗ Incorrect
Only
CaseIterable is the correct protocol to enable iteration over enum cases.What does
Color.allCases return if Color conforms to CaseIterable?✗ Incorrect
allCases returns a collection (array) of all enum cases.Which of these is a valid way to loop through all cases of a
CaseIterable enum Direction?✗ Incorrect
You use
allCases property to get all cases for looping.If you add a new case to a
CaseIterable enum, what happens to allCases?✗ Incorrect
allCases automatically updates to include all enum cases.Can enums with associated values conform to
CaseIterable?✗ Incorrect
Enums with associated values cannot automatically conform to
CaseIterable.Explain how to use
CaseIterable to loop through all cases of an enum in Swift.Think about how you get all cases and then loop over them.
You got /3 concepts.
What are the benefits of making an enum conform to
CaseIterable?Consider how it helps when you want to do something with every case.
You got /4 concepts.
