Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to bind the value in the switch case.
Swift
let point = (3, 4) switch point { case (let [1], 4): print("x is \([1])") default: break }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that doesn't match the print statement.
Forgetting to use 'let' before the variable name.
✗ Incorrect
The 'let x' binds the first element of the tuple to the variable x, which is then used in the print statement.
2fill in blank
mediumComplete the code to bind the associated value in the enum switch.
Swift
enum Result {
case success(Int)
case failure(String)
}
let result = Result.success(10)
switch result {
case .success(let [1]):
print("Success with value \([1])")
case .failure(let message):
print("Failure: \(message)")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not used in the print statement.
Not using 'let' to bind the associated value.
✗ Incorrect
The 'let value' binds the associated Int value from the success case, which is then printed.
3fill in blank
hardFix the error in the switch statement by correctly binding the tuple values.
Swift
let coordinate = (x: 5, y: 0) switch coordinate { case ([1], 0): print("On the x-axis at \([1])") default: print("Not on the x-axis") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'let' before the variable name.
Using the wrong variable name that doesn't match the print statement.
✗ Incorrect
Using 'let x' correctly binds the first element of the tuple to x, which is then used in the print statement.
4fill in blank
hardFill both blanks to bind the tuple values and print them.
Swift
let point = (2, 3) switch point { case ([1], [2]): print("x is \([1]), y is \([2])") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'let' before variable names.
Using variable names that don't match the print statement.
✗ Incorrect
Both 'let x' and 'let y' bind the tuple elements to variables x and y, which are then used in the print statement.
5fill in blank
hardFill all three blanks to bind values in the enum with associated values and print them.
Swift
enum Message {
case text(String, Int)
case image(String)
}
let message = Message.text("Hello", 5)
switch message {
case .text([1], [2]):
print("Text: \([1]), count: \([2])")
case .image(let name):
print("Image named \(name)")
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' inside the parentheses when it's not needed.
Mismatching variable names with the print statement.
✗ Incorrect
The variables 'text' and 'count' are bound without 'let' inside the case pattern, and are used in the print statement.