0
0
Swiftprogramming~20 mins

Why enums are powerful in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Enum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift enum code?

Consider this Swift code using enums with associated values. What will it print?

Swift
enum Device {
    case iPhone(model: String)
    case iPad(model: String)
    case mac
}

let myDevice = Device.iPhone(model: "13 Pro")
switch myDevice {
case .iPhone(let model):
    print("iPhone model: \(model)")
case .iPad(let model):
    print("iPad model: \(model)")
case .mac:
    print("Mac computer")
}
AiPhone model: 13 Pro
BiPad model: 13 Pro
CMac computer
DCompilation error
Attempts:
2 left
💡 Hint

Look at the enum case used to create myDevice and how the switch matches it.

🧠 Conceptual
intermediate
1:30remaining
Why are enums with associated values powerful in Swift?

Which of the following best explains why enums with associated values are powerful in Swift?

AThey allow grouping related values with different types under one type, enabling safer and clearer code.
BThey only store integer values, making them faster than other types.
CThey automatically generate UI elements for each case without extra code.
DThey replace classes and structs entirely in Swift.
Attempts:
2 left
💡 Hint

Think about how enums can hold different kinds of data in one type.

🔧 Debug
advanced
2:00remaining
What error does this Swift enum code produce?

Examine this Swift code snippet. What error will it cause?

Swift
enum Direction {
    case north
    case south
    case east
    case west
}

let dir = Direction.north

switch dir {
case .north:
    print("Going north")
case .south:
    print("Going south")
case .east:
    print("Going east")
// Missing case for .west
}
AError: Cannot use enum cases without raw values
BError: Switch must be exhaustive, missing case for .west
CRuntime error: Unhandled case .west
DNo error, code runs fine
Attempts:
2 left
💡 Hint

Swift requires switch statements on enums to cover all cases or have a default.

🚀 Application
advanced
1:30remaining
How many items are in the resulting enum array?

Given this Swift enum and array, how many elements does allCases contain?

Swift
enum Fruit: CaseIterable {
    case apple
    case banana
    case cherry
}

let allFruits = Fruit.allCases
print(allFruits.count)
ACompilation error
B0
C1
D3
Attempts:
2 left
💡 Hint

Look at how many cases the enum has and what CaseIterable does.

Predict Output
expert
3:00remaining
What is the output of this Swift enum with indirect recursion?

Consider this Swift code using an indirect enum for recursive data. What will it print?

Swift
indirect enum ArithmeticExpression {
    case number(Int)
    case addition(ArithmeticExpression, ArithmeticExpression)
    case multiplication(ArithmeticExpression, ArithmeticExpression)
}

func evaluate(_ expr: ArithmeticExpression) -> Int {
    switch expr {
    case .number(let value):
        return value
    case .addition(let left, let right):
        return evaluate(left) + evaluate(right)
    case .multiplication(let left, let right):
        return evaluate(left) * evaluate(right)
    }
}

let expr = ArithmeticExpression.addition(
    .number(2),
    .multiplication(
        .number(3),
        .number(4)
    )
)

print(evaluate(expr))
A24
B20
C14
DCompilation error due to recursion
Attempts:
2 left
💡 Hint

Calculate the expression step by step: 2 + (3 * 4).