Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an enum named Direction.
Swift
enum [1] {
case north
case south
case east
case west
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names for enum types.
✗ Incorrect
The enum name should be Direction to follow Swift naming conventions.
2fill in blank
mediumComplete the code to create a variable with the enum value east.
Swift
var currentDirection: Direction = Direction.[1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'East' when the enum case is lowercase 'east'.
✗ Incorrect
Enum cases in Swift start with lowercase by convention. The case is east as per the enum declaration.
3fill in blank
hardFix the error in the switch statement to handle all enum cases.
Swift
switch currentDirection {
case .north:
print("Going north")
case .south:
print("Going south")
case [1]:
print("Going east")
default:
print("Unknown direction")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using case without dot notation or missing cases.
✗ Incorrect
The missing case is .east to cover the east direction in the switch.
4fill in blank
hardFill both blanks to create an enum with associated values and assign it.
Swift
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
var productBarcode = Barcode.[1]([2]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up case names or providing wrong value types.
✗ Incorrect
The enum case upc takes four integer values as associated values.
5fill in blank
hardFill all three blanks to create a recursive enum and use it.
Swift
indirect enum ArithmeticExpression {
case number(Int)
case addition([1], [2])
case multiplication([3], ArithmeticExpression)
}
let five = ArithmeticExpression.number(5) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using primitive types instead of the enum type for recursion.
✗ Incorrect
The recursive enum uses ArithmeticExpression as the type for its associated values.