An enum in Swift is a type that defines a group of related values in a clear and type-safe way. It helps organize code by grouping options under one name.
Click to reveal answer
intermediate
How do enums improve code safety in Swift?
Enums restrict values to a defined set, preventing invalid values and reducing bugs. The compiler checks that all cases are handled, making code safer.
Click to reveal answer
intermediate
What is an associated value in Swift enums?
Associated values let each enum case carry extra information. This means each case can store different types or amounts of data, making enums very flexible.
Click to reveal answer
intermediate
How do enums support pattern matching in Swift?
Enums work with Swift's switch statements to match cases easily. This lets you write clear, readable code that handles each enum case explicitly.
Click to reveal answer
beginner
Why are enums better than constants or strings for fixed options?
Enums group related options with clear names and types, avoiding mistakes like typos in strings. They make code easier to read, maintain, and less error-prone.
Click to reveal answer
What does an enum in Swift primarily do?
APerforms arithmetic operations
BCreates a class
CStores user input
DDefines a group of related values
✗ Incorrect
Enums group related values under one type, making code organized and safe.
Which feature allows Swift enums to store extra data with each case?
AProtocols
BInheritance
CAssociated values
DExtensions
✗ Incorrect
Associated values let enum cases carry additional information.
How does Swift ensure all enum cases are handled in code?
AUsing switch statements with exhaustive case handling
BUsing if-else statements only
CBy ignoring unhandled cases
DBy using loops
✗ Incorrect
Switch statements require all enum cases to be handled, improving safety.
Why are enums preferred over strings for fixed options?
AEnums prevent typos and improve code clarity
BStrings are faster
CStrings use less memory
DEnums are harder to read
✗ Incorrect
Enums reduce errors like typos and make code easier to understand.
What Swift feature works well with enums for clear case handling?
AWhile loops
BSwitch statements
CFor loops
DIf statements only
✗ Incorrect
Switch statements allow clear, explicit handling of each enum case.
Explain why enums are powerful in Swift and how they improve code safety and clarity.
Think about how enums limit options and help the compiler check your code.
You got /5 concepts.
Describe how associated values in Swift enums add flexibility compared to simple enums.
Imagine each enum case carrying its own little package of information.
You got /4 concepts.
Practice
(1/5)
1. What is one main reason enums are powerful in Swift?
easy
A. They group related values clearly and safely.
B. They allow any type of value without restrictions.
C. They automatically generate user interfaces.
D. They replace all classes and structs.
Solution
Step 1: Understand enum purpose
Enums group related values into a clear set, making code safer and easier to read.
Step 2: Compare options
Options A, B, and C describe features not true for enums. Only They group related values clearly and safely. correctly states their main power.
Final Answer:
They group related values clearly and safely. -> Option A
Quick Check:
Enums group related values = D [OK]
Hint: Enums group related values safely and clearly [OK]
Common Mistakes:
Thinking enums allow any value type without limits
Confusing enums with UI components
Believing enums replace classes and structs
2. Which of the following is the correct syntax to define an enum in Swift?
easy
A. enum Direction: String { north, south, east, west }
B. enum Direction = { north, south, east, west }
C. enum Direction { north; south; east; west }
D. enum Direction { case north, south, east, west }
Solution
Step 1: Recall enum syntax
In Swift, enums use the keyword 'enum' followed by cases declared with 'case' keyword separated by commas.
Step 2: Check each option
enum Direction { case north, south, east, west } uses correct syntax. enum Direction = { north, south, east, west } uses '=' which is invalid. enum Direction: String { north, south, east, west } misses 'case' keyword. enum Direction { north; south; east; west } uses semicolons incorrectly.
Final Answer:
enum Direction { case north, south, east, west } -> Option D
Quick Check:
Correct enum syntax uses 'case' keyword = C [OK]
Hint: Remember enum cases start with 'case' keyword [OK]
Common Mistakes:
Using '=' instead of '{ }' for enum body
Omitting 'case' keyword before cases
Separating cases with semicolons instead of commas
3. What will be the output of this Swift code?
enum Result {
case success(String)
case failure(String)
}
let outcome = Result.success("Done")
switch outcome {
case .success(let message):
print("Success: \(message)")
case .failure(let error):
print("Failure: \(error)")
}
medium
A. Success: error
B. Failure: Done
C. Success: Done
D. Compilation error
Solution
Step 1: Understand enum with associated values
The enum Result has two cases with associated String values. The variable 'outcome' is assigned .success("Done").
Step 2: Analyze switch statement
The switch matches .success case, extracts 'message' as "Done", and prints "Success: Done".
Final Answer:
Success: Done -> Option C
Quick Check:
Matching .success prints message = A [OK]
Hint: Match enum cases with associated values carefully [OK]
Common Mistakes:
Confusing success and failure cases
Forgetting to extract associated values
Expecting compilation error due to associated values
4. Identify the error in this Swift enum code:
enum Status {
case ready
case waiting
case error(String)
}
let current = Status.error
print(current)
medium
A. Enum cases cannot have associated values.
B. Missing associated value for 'error' case.
C. Cannot print enum values directly.
D. Enum must have raw values.
Solution
Step 1: Check enum definition
The 'error' case requires a String associated value, but 'current' is assigned without one.
Step 2: Understand assignment error
Assigning 'Status.error' without a String causes a compile-time error because associated values are mandatory.
Final Answer:
Missing associated value for 'error' case. -> Option B
Quick Check:
Associated values must be provided = A [OK]
Hint: Provide associated values when required by enum case [OK]
Common Mistakes:
Assigning enum case with associated value without value
Thinking enums cannot have associated values
Assuming printing enum directly causes error
5. Given this enum with associated values, how can you extract the associated String from a variable of type Response?
enum Response {
case data(String)
case error(Int, String)
}
let reply = Response.error(404, "Not Found")
Which code snippet correctly extracts the error message?
hard
A. if case let .error(code, message) = reply { print(message) }
B. if reply == .error { print("Error") }
C. switch reply { case .data(let msg): print(msg) default: break }
D. let message = reply.errorMessage
Solution
Step 1: Understand enum with multiple associated values
The 'error' case has two associated values: an Int and a String. To get the message, both must be extracted.
Step 2: Analyze extraction methods
if case let .error(code, message) = reply { print(message) } uses pattern matching with 'if case let' to extract both values and print the message. Options A, C, and D are invalid syntax or incomplete.
Final Answer:
if case let .error(code, message) = reply { print(message) } -> Option A
Quick Check:
Pattern match to extract associated values = B [OK]
Hint: Use 'if case let' to extract associated values from enums [OK]
Common Mistakes:
Trying to compare enum with associated values directly
Ignoring associated values in switch or if
Assuming enum has properties like 'errorMessage'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.