An enum with associated values lets each case store additional data of different types, like a package holding extra info for each option.
Click to reveal answer
beginner
How do you define an enum case with associated values in Swift?
Use parentheses after the case name to specify the types of associated values, for example: case success(data: String)
Click to reveal answer
intermediate
Why use enums with associated values instead of simple enums?
They let you attach extra information to each case, making your code more flexible and expressive, like adding details to a label on a box.
Click to reveal answer
intermediate
How do you extract associated values from an enum in Swift?
Use a switch statement to match each case and bind its associated values to constants or variables for use inside the case block.
Click to reveal answer
intermediate
Can enum cases with associated values have different types?
Yes, each case can have different types and numbers of associated values, allowing diverse data to be stored depending on the case.
Click to reveal answer
Which Swift keyword defines an enum with associated values?
Aclass
Bstruct
Cenum
Dfunc
✗ Incorrect
The keyword 'enum' defines enumerations, including those with associated values.
How do you declare an enum case with an associated String value?
Acase example = String
Bcase example(String)
Ccase example: String
Dcase example -> String
✗ Incorrect
Use parentheses after the case name to specify the associated value type.
What Swift construct is commonly used to access associated values?
Aswitch statement
Bwhile loop
Cfor loop
Dif statement
✗ Incorrect
A switch statement lets you match enum cases and extract their associated values.
Can different enum cases have different associated value types?
ANo
BOnly if declared in the same line
COnly if they are the same size
DYes
✗ Incorrect
Each case can have its own unique associated value types.
What is a real-life example of using enums with associated values?
ARepresenting a network response with success(data) or failure(error)
BStoring a list of numbers
CCreating a button UI
DDefining a color palette
✗ Incorrect
Enums with associated values can model responses carrying data or errors.
Explain how enums with associated values improve code flexibility in Swift.
Think about how you can store more info with each enum option.
You got /3 concepts.
Describe the steps to define and use an enum with associated values in a Swift app.
Consider declaration, creation, and usage phases.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using enum with associated values in Swift?
easy
A. To store extra data along with each enum case
B. To create a class with multiple methods
C. To define global constants
D. To write loops more efficiently
Solution
Step 1: Understand enum basics
An enum groups related values under one type.
Step 2: Recognize associated values role
Associated values let each case hold extra data, like a number or string.
Final Answer:
To store extra data along with each enum case -> Option A
Quick Check:
Enums with associated values store extra data [OK]
Hint: Remember: associated values add data to enum cases [OK]
Common Mistakes:
Confusing enums with classes
Thinking enums only hold fixed values
Mixing associated values with raw values
2. Which of the following is the correct syntax to define an enum case with an associated value of type String?
easy
A. case name = String
B. case name(String)
C. case name: String
D. case name -> String
Solution
Step 1: Recall enum case syntax with associated values
Use parentheses after the case name to specify the type of associated value.
Step 2: Match correct syntax
Only case name(String) uses parentheses correctly.
Final Answer:
case name(String) -> Option B
Quick Check:
Use parentheses for associated value types [OK]
Hint: Use parentheses to declare associated value types [OK]
Common Mistakes:
Using colon instead of parentheses
Assigning value with equals sign
Using arrow notation
3. Given the enum and code below, what will be printed?
enum Result {
case success(String)
case failure(Int)
}
let result = Result.success("Done")
switch result {
case .success(let message):
print(message)
case .failure(let code):
print("Error \(code)")
}
medium
A. failure
B. Error Done
C. Done
D. Error 0
Solution
Step 1: Identify enum case used
The variable result is .success("Done").
Step 2: Follow switch matching
The switch matches .success(let message) and prints the message "Done".
Final Answer:
Done -> Option C
Quick Check:
Matching case prints associated string [OK]
Hint: Match enum cases to print associated values [OK]
Common Mistakes:
Printing the enum case name instead of value
Confusing success and failure cases
Not using let to extract associated value
4. What is wrong with this enum and switch code?
enum Direction {
case up(Int)
case down(Int)
}
let dir = Direction.up(10)
switch dir {
case .up:
print("Going up")
case .down:
print("Going down")
}
medium
A. Switch cases must extract associated values with let
B. Enum cases cannot have associated values
C. Variable dir is not initialized correctly
D. Switch must have a default case
Solution
Step 1: Check enum cases with associated values
Cases up and down have Int associated values.
Step 2: Analyze switch cases
Switch cases .up and .down do not extract the Int value with let, causing a compile error.
Final Answer:
Switch cases must extract associated values with let -> Option A
Quick Check:
Extract associated values in switch cases [OK]
Hint: Always use let to get associated values in switch [OK]
Common Mistakes:
Ignoring associated values in switch
Thinking default case is mandatory
Wrong enum initialization
5. You want to model a network response enum with cases success carrying a String message, failure carrying an Int error code, and loading with no data. Which is the correct enum definition?
hard
A. enum NetworkResponse {
case success(String)
case failure(String)
case loading(Int)
}
B. enum NetworkResponse {
case success
case failure
case loading
}
C. enum NetworkResponse {
case success(String, Int)
case failure(Int)
case loading
}
D. enum NetworkResponse {
case success(String)
case failure(Int)
case loading
}
Solution
Step 1: Match each case with correct associated value
success needs a String, failure an Int, loading no data.
Step 2: Check each option
enum NetworkResponse {
case success(String)
case failure(Int)
case loading
} matches exactly: success(String), failure(Int), loading without value.
Final Answer:
enum NetworkResponse {
case success(String)
case failure(Int)
case loading
} -> Option D
Quick Check:
Define cases with correct associated types [OK]
Hint: Match each case to its exact associated value type [OK]
Common Mistakes:
Adding wrong types to cases
Combining multiple values in one case incorrectly
Assigning associated values to cases that should have none
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.