0
0
Swiftprogramming~10 mins

Switch with value binding in Swift - Interactive Code Practice

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

Complete 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'
Ax
By
Cvalue
Dz
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.
2fill in blank
medium

Complete 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'
Aerror
Bvalue
Cresult
Dnumber
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.
3fill in blank
hard

Fix 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'
Alet x
Bx
Clet y
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting 'let' before the variable name.
Using the wrong variable name that doesn't match the print statement.
4fill in blank
hard

Fill 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'
Alet x
Blet y
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Not using 'let' before variable names.
Using variable names that don't match the print statement.
5fill in blank
hard

Fill 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'
Alet text
Blet count
Ccount
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'let' inside the parentheses when it's not needed.
Mismatching variable names with the print statement.