0
0
Swiftprogramming~10 mins

Multiple optional 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 unwrap the optional 'name' safely.

Swift
if let [1] = name {
    print("Hello, \([1])!")
}
Drag options to blanks, or click blank then click option'
Agreeting
Bname
Cperson
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name without unwrapping the optional.
Forgetting to unwrap the optional before using it.
2fill in blank
medium

Complete the code to unwrap both optionals 'firstName' and 'lastName' in a single statement.

Swift
if let [1] = firstName, let last = lastName {
    print("Full name: \([1]) \(last)")
}
Drag options to blanks, or click blank then click option'
Afirst
Bname
CfirstName
DfirstN
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name that does not match the optional.
Not separating the bindings with commas.
3fill in blank
hard

Fix the error in the multiple optional binding statement.

Swift
if let age = age, let [1] = city {
    print("Age: \(age), City: \([1])")
}
Drag options to blanks, or click blank then click option'
Acity
Blocation
Cplace
Dtown
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name that causes a runtime error.
Not unwrapping the optional before use.
4fill in blank
hard

Fill both blanks to unwrap 'username' and 'password' optionals and print them.

Swift
if let [1] = username, let [2] = password {
    print("User: \([1]), Pass: \([2])")
}
Drag options to blanks, or click blank then click option'
Auser
Busername
Cpass
Dpassword
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names that don't match the print statement.
Forgetting to unwrap both optionals.
5fill in blank
hard

Fill all three blanks to unwrap 'email', 'phone', and 'address' optionals and print them.

Swift
if let [1] = email, let [2] = phone, let [3] = address {
    print("Email: \([1]), Phone: \([2]), Address: \([3])")
}
Drag options to blanks, or click blank then click option'
Aemail
Bphone
Caddress
Dcontact
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names that cause errors in the print statement.
Not unwrapping all optionals before use.