Bird
Raised Fist0
Swiftprogramming~20 mins

Iterating enum cases with CaseIterable in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CaseIterable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output of iterating enum cases with CaseIterable
What is the output of this Swift code that iterates over enum cases using CaseIterable?
Swift
enum Direction: CaseIterable {
    case north, south, east, west
}

for direction in Direction.allCases {
    print(direction)
}
ADirection.north\nDirection.south\nDirection.east\nDirection.west
B0\n1\n2\n3
Cnorth\nsouth\neast\nwest
Dnorth south east west
Attempts:
2 left
💡 Hint
Remember that printing an enum case prints its name by default.
🧠 Conceptual
intermediate
1:00remaining
Why use CaseIterable with enums?
Why is the CaseIterable protocol useful when working with enums in Swift?
AIt enables enums to inherit from other enums.
BIt automatically provides a collection of all enum cases for iteration.
CIt allows enums to have stored properties.
DIt makes enums conform to Codable automatically.
Attempts:
2 left
💡 Hint
Think about how you can get all enum cases without manually listing them.
🔧 Debug
advanced
2:00remaining
Fix the error when iterating enum without CaseIterable
This code tries to iterate over enum cases but causes an error. Which option fixes the error?
Swift
enum Color {
    case red, green, blue
}

for color in Color.allCases {
    print(color)
}
AAdd raw values to the enum cases.
BChange 'Color.allCases' to 'Color.cases'.
CReplace 'for' loop with 'while' loop.
DAdd ': CaseIterable' to the enum declaration.
Attempts:
2 left
💡 Hint
The error is because 'allCases' is not available without a protocol conformance.
📝 Syntax
advanced
1:30remaining
Correct syntax for custom allCases implementation
Which option correctly implements a custom allCases property for an enum conforming to CaseIterable?
Swift
enum Fruit: CaseIterable {
    case apple, banana, cherry
    // custom allCases implementation
}
Astatic var allCases: [Fruit] { return [.apple, .banana, .cherry] }
Bvar allCases: [Fruit] { return [.apple, .banana, .cherry] }
Cstatic func allCases() -> [Fruit] { return [.apple, .banana, .cherry] }
Dlet allCases = [.apple, .banana, .cherry]
Attempts:
2 left
💡 Hint
The allCases property must be static and return an array of enum cases.
🚀 Application
expert
1:00remaining
Count enum cases using CaseIterable
Given this enum, what is the value of 'count' after the code runs?
Swift
enum Status: CaseIterable {
    case pending, approved, rejected, cancelled
}

let count = Status.allCases.count
A4
B3
CCompilation error
D0
Attempts:
2 left
💡 Hint
Count returns the number of elements in the allCases collection.

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