📖 Scenario: Imagine you are building a simple app that shows different messages based on the weather condition. The weather can be sunny, rainy, or snowy.
🎯 Goal: You will create an enum to represent weather conditions, then use a switch statement with pattern matching to print the correct message for each weather type.
📋 What You'll Learn
Create an enum called Weather with cases sunny, rainy, and snowy
Create a variable called today and assign it the Weather.sunny value
Use a switch statement on today with cases for each weather type
Print a message inside each case describing the weather
💡 Why This Matters
🌍 Real World
Enums and switch statements are used in apps to handle different states or options clearly and safely.
💼 Career
Understanding enums and pattern matching is important for Swift developers building iOS apps with clear and maintainable code.
Progress0 / 4 steps
1
Create the Weather enum
Create an enum called Weather with three cases: sunny, rainy, and snowy.
Swift
Hint
Use the enum keyword followed by the name Weather. Inside curly braces, list the cases with the case keyword.
2
Create a variable for today's weather
Create a variable called today and assign it the value Weather.sunny.
Swift
Hint
Use var today = Weather.sunny to create the variable.
3
Use switch to match the weather
Write a switch statement on the variable today. Add cases for .sunny, .rainy, and .snowy. Inside each case, write a print statement with a message describing the weather, for example, print("It's sunny today!").
Swift
Hint
Use switch today { case .sunny: ... } and print messages inside each case.
4
Print the weather message
Run the program to print the message for today's weather. The output should be exactly: It's sunny today!
Swift
Hint
Make sure you run the program and see the message printed exactly as shown.
Practice
(1/5)
1. What is the main purpose of using an enum with associated values in Swift?
easy
A. To group related values and store extra data in each case
B. To create a class with multiple methods
C. To define a function that returns multiple values
D. To declare a variable that can only hold integers
Solution
Step 1: Understand enum basics
An enum groups related values under one type, making code organized.
Step 2: Recognize associated values
Enums with associated values can store extra data for each case, like a label or number.
Final Answer:
To group related values and store extra data in each case -> Option A
Quick Check:
Enum with associated values = group + extra data [OK]
Hint: Enums group related data with extra info easily [OK]
Common Mistakes:
Confusing enums with classes
Thinking enums only hold fixed values
Assuming enums are functions
2. Which of the following is the correct syntax to match an enum case with associated values using switch in Swift?
easy
A. case .someCase(let value):
B. case someCase = value:
C. case .someCase => value:
D. case someCase(value) {
Solution
Step 1: Recall switch case syntax for enums
In Swift, to match an enum case with associated values, use case .caseName(let variable):.
Step 2: Identify correct pattern
case .someCase(let value): uses the correct dot syntax and binding with let.
Final Answer:
case .someCase(let value): -> Option A
Quick Check:
Switch enum case with let binding = case .someCase(let value): [OK]
Hint: Use dot and let to bind enum associated values [OK]
Common Mistakes:
Missing dot before case name
Using = instead of :
Wrong arrow or braces syntax
3. What will be the output of this Swift code?
enum Direction {
case north, south, east, west
}
let dir = Direction.east
switch dir {
case .north:
print("Going North")
case .south:
print("Going South")
case .east:
print("Going East")
case .west:
print("Going West")
}
medium
A. No output
B. Going North
C. Going West
D. Going East
Solution
Step 1: Identify enum value assigned
The variable dir is assigned Direction.east.
Step 2: Match switch case
The switch matches .east case and prints "Going East".
Final Answer:
Going East -> Option D
Quick Check:
dir = east prints "Going East" [OK]
Hint: Match enum value to switch case to find output [OK]
Common Mistakes:
Confusing enum case names
Forgetting to include all cases
Assuming default case runs
4. Identify the error in this Swift code using enum and switch:
enum Result {
case success(Int)
case failure(String)
}
let res = Result.success(10)
switch res {
case .success(let value):
print("Value: \(value)")
case failure(let message):
print("Error: \(message)")
}
medium
A. Switch statement missing default case
B. Wrong associated value type for 'success'
C. Missing dot before 'failure' in switch case
D. Enum cases cannot have associated values
Solution
Step 1: Check switch case syntax
In switch, enum cases must be prefixed with a dot, like .failure.
Step 2: Identify missing dot
The second case uses failure without a dot, causing a syntax error.
Final Answer:
Missing dot before 'failure' in switch case -> Option C
Quick Check:
Enum case in switch needs dot prefix [OK]
Hint: Always prefix enum cases with dot in switch [OK]
Common Mistakes:
Omitting dot before enum case in switch
Confusing associated value types
Forgetting to handle all cases
5. Given this enum and function, what will describeShape(.rectangle(width: 5, height: 10)) print?
enum Shape {
case circle(radius: Double)
case rectangle(width: Double, height: Double)
case point
}
func describeShape(_ shape: Shape) {
switch shape {
case .circle(let r):
print("Circle with radius \(r)")
case .rectangle(let w, let h):
print("Rectangle \(w) x \(h)")
case .point:
print("Just a point")
}
}
hard
A. Circle with radius 5.0
B. Rectangle 5.0 x 10.0
C. Just a point
D. Syntax error in switch
Solution
Step 1: Identify enum case and associated values
The input is .rectangle(width: 5, height: 10), so width = 5 and height = 10.
Step 2: Match switch case and print
The switch matches .rectangle(let w, let h) and prints "Rectangle 5.0 x 10.0".
Final Answer:
Rectangle 5.0 x 10.0 -> Option B
Quick Check:
Rectangle case prints dimensions [OK]
Hint: Match enum case and extract all associated values [OK]
Common Mistakes:
Using wrong variable names in case
Missing multiple associated values in pattern
Assuming default case runs
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.