0
0
Swiftprogramming~10 mins

Why enums are powerful in Swift - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AMove
Bdirection
CDirections
DDirection
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or plural names for enum types.
2fill in blank
medium

Complete 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'
Aeast
BEast
CNorth
Dwest
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'East' when the enum case is lowercase 'east'.
3fill in blank
hard

Fix 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'
A.west
B.east
Ceast
Dwest
Attempts:
3 left
💡 Hint
Common Mistakes
Using case without dot notation or missing cases.
4fill in blank
hard

Fill 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'
AqrCode
B"ABCDEFGHIJKLMNOP"
Cupc
D12345, 67890, 1112, 1314
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up case names or providing wrong value types.
5fill in blank
hard

Fill 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'
AArithmeticExpression
BInt
DNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using primitive types instead of the enum type for recursion.