Bird
Raised Fist0
Swiftprogramming~5 mins

Iterating enum cases with CaseIterable in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
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.
Click to reveal answer
beginner
How do you make an enum conform to CaseIterable?
Add CaseIterable after the enum name, like enum Direction: CaseIterable { ... }.
Click to reveal answer
beginner
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.
Click to reveal answer
intermediate
What type is allCases in a CaseIterable enum?
It is a collection (usually an array) of all enum cases, with type [EnumName].
Click to reveal answer
beginner
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.
Click to reveal answer
How do you declare an enum named Color that supports iterating all its cases?
Aenum Color: CaseIterable { case red, green, blue }
Benum Color { case red, green, blue }
Cenum Color: Iterable { case red, green, blue }
Denum Color: IterableCase { case red, green, blue }
What does Color.allCases return if Color conforms to CaseIterable?
AAn array of all Color enum cases
BA single Color case
CA string listing all cases
DAn integer count of cases
Which of these is a valid way to loop through all cases of a CaseIterable enum Direction?
Afor dir in Direction.cases { print(dir) }
Bfor dir in Direction { print(dir) }
Cfor dir in Direction.allCases { print(dir) }
Dfor dir in Direction.iterate() { print(dir) }
If you add a new case to a CaseIterable enum, what happens to allCases?
AIt excludes the new case
BYou must update <code>allCases</code> manually
CIt breaks the code
DIt automatically includes the new case
Can enums with associated values conform to CaseIterable?
AYes, all enums can conform
BNo, only enums without associated values can conform
COnly if associated values are Int
DOnly if associated values are String
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.

      Practice

      (1/5)
      1. What does the CaseIterable protocol allow you to do with an enum in Swift?
      easy
      A. Make enum cases private
      B. Access all enum cases as a collection
      C. Add stored properties to enum cases
      D. Change enum cases at runtime

      Solution

      1. Step 1: Understand the purpose of CaseIterable

        The CaseIterable protocol provides a way to access all cases of an enum as a collection.
      2. Step 2: Identify what CaseIterable does not do

        It does not allow changing cases, adding stored properties, or changing access levels.
      3. Final Answer:

        Access all enum cases as a collection -> Option B
      4. Quick Check:

        CaseIterable = Access all cases [OK]
      Hint: Remember: CaseIterable gives you all enum cases easily [OK]
      Common Mistakes:
      • Thinking CaseIterable allows modifying enum cases
      • Confusing CaseIterable with adding properties
      • Assuming CaseIterable changes access control
      2. Which of the following is the correct way to declare an enum that supports iteration over all its cases using CaseIterable?
      easy
      A. enum Direction: CaseIterable { case north, south, east, west }
      B. enum Direction { case north, south, east, west }
      C. enum Direction: Iterable { case north, south, east, west }
      D. enum Direction: CaseIterable() { case north, south, east, west }

      Solution

      1. Step 1: Check correct protocol conformance syntax

        In Swift, you conform to a protocol by listing it after a colon without parentheses.
      2. Step 2: Identify the correct protocol name

        The correct protocol is CaseIterable, not Iterable, and no parentheses are used.
      3. Final Answer:

        enum Direction: CaseIterable { case north, south, east, west } -> Option A
      4. Quick Check:

        Correct syntax = enum Direction: CaseIterable { case north, south, east, west } [OK]
      Hint: Use ': CaseIterable' without parentheses to conform [OK]
      Common Mistakes:
      • Adding parentheses after CaseIterable
      • Using wrong protocol name like Iterable
      • Forgetting to conform to CaseIterable
      3. Given the enum below, what will be printed by the code?
      enum Fruit: CaseIterable {
          case apple, banana, cherry
      }
      
      for fruit in Fruit.allCases {
          print(fruit)
      }
      medium
      A. apple banana cherry
      B. ["apple", "banana", "cherry"]
      C. apple banana cherry
      D. Fruit.apple Fruit.banana Fruit.cherry

      Solution

      1. Step 1: Understand the for loop over allCases

        The loop iterates over each case in the enum and prints it on a new line.
      2. Step 2: Recognize print behavior in Swift

        Each call to print outputs the case name followed by a newline.
      3. Final Answer:

        apple banana cherry -> Option C
      4. Quick Check:

        Loop prints each case on separate lines [OK]
      Hint: print() adds new line after each output [OK]
      Common Mistakes:
      • Assuming all cases print on one line
      • Expecting array output instead of separate lines
      • Confusing enum case names with full type names
      4. What is wrong with this code snippet?
      enum Color: CaseIterable {
          case red, green, blue
      }
      
      for color in Color {
          print(color)
      }
      medium
      A. The enum cases must be strings
      B. The enum must be a class to iterate
      C. CaseIterable requires a raw value type
      D. You must use Color.allCases to iterate, not Color directly

      Solution

      1. Step 1: Identify how to iterate over enum cases

        You cannot iterate over the enum type itself; you must use allCases property.
      2. Step 2: Understand CaseIterable usage

        CaseIterable provides allCases which returns a collection of all enum cases for iteration.
      3. Final Answer:

        You must use Color.allCases to iterate, not Color directly -> Option D
      4. Quick Check:

        Use allCases for iteration [OK]
      Hint: Always loop over Enum.allCases, not Enum itself [OK]
      Common Mistakes:
      • Trying to loop over enum type directly
      • Thinking CaseIterable needs raw values
      • Assuming enum must be a class
      5. You want to create a menu that lists all payment methods defined in this enum:
      enum PaymentMethod: CaseIterable {
          case cash, creditCard, paypal, applePay
      }

      Which code snippet correctly prints each payment method with a number starting from 1?
      hard
      A. for (index, method) in PaymentMethod.allCases.enumerated() { print("\(index + 1). \(method)") }
      B. for method in PaymentMethod.allCases { print("\(method + 1). \(method)") }
      C. for index in 1...PaymentMethod.allCases.count { print("\(index). \(PaymentMethod.allCases[index])") }
      D. for method in PaymentMethod { print("\(method.rawValue + 1). \(method)") }

      Solution

      1. Step 1: Use enumerated() to get index and value

        The enumerated() method returns pairs of (index, element) for the collection.
      2. Step 2: Adjust index to start from 1 and print

        Since enumerated indexes start at 0, add 1 to show numbering from 1.
      3. Final Answer:

        for (index, method) in PaymentMethod.allCases.enumerated() { print("\(index + 1). \(method)") } -> Option A
      4. Quick Check:

        Use enumerated() + index+1 for numbering [OK]
      Hint: Use enumerated() and add 1 to index for numbering [OK]
      Common Mistakes:
      • Trying to add 1 directly to enum case
      • Indexing allCases with 1-based index (off by one)
      • Looping over enum type instead of allCases
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard