Bird
Raised Fist0
Swiftprogramming~5 mins

Enum declaration and cases in Swift - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is an enum in Swift?
An enum (short for enumeration) is a way to group related values under a common type. It helps organize options or choices in your code.
Click to reveal answer
beginner
How do you declare an enum with cases in Swift?
Use the enum keyword, then list cases inside curly braces. For example:
enum Direction {
  case north
  case south
  case east
  case west
}
Click to reveal answer
intermediate
Can enum cases have associated values in Swift?
Yes! Enum cases can store extra information called associated values. For example:
enum Barcode {
  case upc(Int, Int, Int, Int)
  case qrCode(String)
}
Click to reveal answer
beginner
How do you use an enum case in Swift code?
You create a value by writing the enum name, a dot, then the case name. For example:<br><pre>let direction = Direction.north</pre>
Click to reveal answer
beginner
What is the benefit of using enums instead of strings or numbers for fixed options?
Enums make your code safer and clearer. They prevent mistakes like typos and make it easy to see all possible options in one place.
Click to reveal answer
How do you declare an enum named Color with cases red, green, and blue in Swift?
Aenum Color { case red, green, blue }
Bclass Color { var red, green, blue }
Cstruct Color { let red, green, blue }
Denum Color = [red, green, blue]
Which of these is a valid way to create a value of enum Direction with case east?
Alet d = Direction->east
Blet d = Direction.east
Clet d = Direction[east]
Dlet d = Direction.east()
Can enum cases in Swift have extra data attached?
AOnly if declared as classes
BNo, enums only hold fixed names
CYes, but only strings
DYes, using associated values
What keyword starts an enum declaration in Swift?
Aenum
Bclass
Cstruct
Dcase
Why use enums instead of strings for fixed options?
ABecause enums use less memory
BBecause strings are slower
CTo avoid typos and improve code clarity
DBecause strings cannot be compared
Explain how to declare an enum with multiple cases in Swift and how to create a value from it.
Think about the basic syntax and how you pick a case.
You got /4 concepts.
    Describe what associated values are in Swift enums and give an example.
    Imagine a case that holds more than just a name.
    You got /3 concepts.

      Practice

      (1/5)
      1. What is the purpose of an enum in Swift?
      easy
      A. To group related values under one name
      B. To create a function
      C. To declare a variable
      D. To write a loop

      Solution

      1. Step 1: Understand what enums represent

        Enums group related values together to make code clearer and safer.
      2. Step 2: Compare options with enum purpose

        Only To group related values under one name correctly describes grouping related values under one name.
      3. Final Answer:

        To group related values under one name -> Option A
      4. Quick Check:

        Enum purpose = Group related values [OK]
      Hint: Enums group related values clearly and safely [OK]
      Common Mistakes:
      • Confusing enums with functions
      • Thinking enums declare variables
      • Mixing enums with loops
      2. Which of the following is the correct way to declare an enum named Direction with cases north, south, east, and west?
      easy
      A. enum Direction = { north, south, east, west }
      B. enum Direction: north, south, east, west {}
      C. enum Direction { case north, south, east, west }
      D. enum Direction { north; south; east; west }

      Solution

      1. Step 1: Recall Swift enum syntax

        Enums use the keyword enum, followed by the name and curly braces containing case declarations separated by commas.
      2. Step 2: Check each option's syntax

        enum Direction { case north, south, east, west } uses correct syntax: enum Direction { case north, south, east, west }. Others use invalid syntax.
      3. Final Answer:

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

        Correct enum syntax = enum Direction { case north, south, east, west } [OK]
      Hint: Use 'case' keyword with comma-separated values inside braces [OK]
      Common Mistakes:
      • Using '=' instead of braces
      • Omitting 'case' keyword
      • Separating cases with semicolons
      3. What will be the output of this Swift code?
      enum Light {
          case red, yellow, green
      }
      
      let current = Light.red
      switch current {
      case .red:
          print("Stop")
      case .yellow:
          print("Caution")
      case .green:
          print("Go")
      }
      medium
      A. No output
      B. Go
      C. Caution
      D. Stop

      Solution

      1. Step 1: Identify the enum case assigned

        The variable current is assigned Light.red.
      2. Step 2: Match the switch case for current

        The switch matches .red and prints "Stop".
      3. Final Answer:

        Stop -> Option D
      4. Quick Check:

        Light.red triggers "Stop" output [OK]
      Hint: Match enum case in switch to get output [OK]
      Common Mistakes:
      • Confusing enum cases with strings
      • Forgetting dot prefix in switch
      • Assuming default output
      4. Identify the error in this enum declaration:
      enum Weather {
          case sunny
          case rainy
          case windy
          case
      }
      medium
      A. Missing case name after 'case' keyword
      B. Enum name is invalid
      C. Curly braces are missing
      D. Enum cases should be strings

      Solution

      1. Step 1: Review each enum case line

        The last line has case without a case name, which is invalid syntax.
      2. Step 2: Confirm other parts are correct

        Enum name and braces are correct; cases do not need to be strings.
      3. Final Answer:

        Missing case name after 'case' keyword -> Option A
      4. Quick Check:

        Every 'case' must have a name [OK]
      Hint: Each 'case' must have a name after it [OK]
      Common Mistakes:
      • Leaving 'case' empty
      • Thinking cases need quotes
      • Missing braces around enum
      5. You want to create an enum Planet with cases for mercury, venus, and earth. Then, write a function that returns a friendly message for each planet. Which code correctly implements this?
      hard
      A. enum Planet { mercury, venus, earth } func message(planet: Planet) -> String { if planet == mercury { return "Closest to the Sun" } else if planet == venus { return "Second planet" } else { return "Our home" } }
      B. enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case .mercury: return "Closest to the Sun" case .venus: return "Second planet" case .earth: return "Our home" } }
      C. enum Planet { case mercury, venus, earth } func message(planet: Planet) -> String { if planet == mercury { return "Closest to the Sun" } else if planet == venus { return "Second planet" } else { return "Our home" } }
      D. enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case mercury: return "Closest to the Sun" case venus: return "Second planet" case earth: return "Our home" } }

      Solution

      1. Step 1: Check enum declaration syntax

        Options A, C, and D declare enum correctly with case keyword; B misses it, so B is invalid.
      2. Step 2: Check function parameter and switch usage

        enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case .mercury: return "Closest to the Sun" case .venus: return "Second planet" case .earth: return "Our home" } } uses switch with dot syntax for cases and correct function signature. enum Planet { case mercury, venus, earth } func message(planet: Planet) -> String { if planet == mercury { return "Closest to the Sun" } else if planet == venus { return "Second planet" } else { return "Our home" } } uses if-else without dot syntax (.mercury), causing compile error. enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case mercury: return "Closest to the Sun" case venus: return "Second planet" case earth: return "Our home" } } misses dot prefix in switch cases, causing error.
      3. Final Answer:

        enum Planet { case mercury, venus, earth } func message(for planet: Planet) -> String { switch planet { case .mercury: return "Closest to the Sun" case .venus: return "Second planet" case .earth: return "Our home" } } -> Option B
      4. Quick Check:

        Enum with 'case' and switch with dot cases [OK]
      Hint: Use 'case' keyword and dot prefix in switch cases [OK]
      Common Mistakes:
      • Omitting 'case' keyword in enum
      • Missing dot prefix in switch cases
      • Using if without dot prefix for enum cases
      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