📖 Scenario: You are creating a simple app that tracks the weather condition for the day. You want to use an enum to represent different weather types.
🎯 Goal: Build a Swift program that declares an enum called Weather with specific cases, then prints a message based on a selected weather case.
📋 What You'll Learn
Declare an enum named Weather with cases sunny, rainy, and cloudy
Create a variable named todayWeather and assign it the sunny case of Weather
Use a switch statement on todayWeather to print a message for each weather case
Print the message to the console
💡 Why This Matters
🌍 Real World
Enums help represent fixed sets of related values, like weather types, user roles, or app states, making code easier to read and maintain.
💼 Career
Understanding enums is essential for Swift developers building iOS apps, as they are used widely for managing states and options clearly.
Progress0 / 4 steps
1
Declare the enum with weather cases
Declare an enum named Weather with these exact cases: sunny, rainy, and cloudy.
Swift
Hint
Use the enum keyword followed by the name Weather. Inside curly braces, list each case with the case keyword.
2
Create a variable for today's weather
Create a variable named todayWeather and assign it the sunny case of the Weather enum.
Swift
Hint
Use var todayWeather = Weather.sunny to create and assign the variable.
3
Use a switch statement to handle weather cases
Write a switch statement on todayWeather with cases .sunny, .rainy, and .cloudy. For each case, assign a message string describing the weather to a variable named message. Use these exact messages: "It's a bright sunny day!", "Don't forget your umbrella.", and "Looks a bit cloudy today." respectively.
Swift
Hint
Use switch todayWeather { case .sunny: ... } and assign the exact messages to message.
4
Print the weather message
Write a print statement to display the message variable.
Swift
Hint
Use print(message) to show the message on the screen.
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
Step 1: Understand what enums represent
Enums group related values together to make code clearer and safer.
Step 2: Compare options with enum purpose
Only To group related values under one name correctly describes grouping related values under one name.
Final Answer:
To group related values under one name -> Option A
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
Step 1: Recall Swift enum syntax
Enums use the keyword enum, followed by the name and curly braces containing case declarations separated by commas.
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.
Final Answer:
enum Direction { case north, south, east, west } -> Option C
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
Step 1: Identify the enum case assigned
The variable current is assigned Light.red.
Step 2: Match the switch case for current
The switch matches .red and prints "Stop".
Final Answer:
Stop -> Option D
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
Step 1: Review each enum case line
The last line has case without a case name, which is invalid syntax.
Step 2: Confirm other parts are correct
Enum name and braces are correct; cases do not need to be strings.
Final Answer:
Missing case name after 'case' keyword -> Option A
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
Step 1: Check enum declaration syntax
Options A, C, and D declare enum correctly with case keyword; B misses it, so B is invalid.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.