Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an optional String variable named name.
iOS Swift
var name: [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the question mark and declaring it as a normal String.
Using the word Optional without the generic type.
✗ Incorrect
In Swift, to declare an optional String, you add a question mark after the type: String?.
2fill in blank
mediumComplete the code to safely unwrap the optional variable name using if let.
iOS Swift
if let unwrappedName = [1] { print("Hello, \(unwrappedName)!") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the unwrapped variable name inside the if let condition.
Forcing unwrap with exclamation mark instead of safe unwrapping.
✗ Incorrect
Use if let followed by the optional variable to unwrap it safely.
3fill in blank
hardFix the error in force unwrapping the optional age variable.
iOS Swift
let age: Int? = 25 let unwrappedAge = [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the exclamation mark and causing a type mismatch.
Using optional chaining instead of force unwrapping.
✗ Incorrect
To force unwrap an optional, use the exclamation mark after the variable name.
4fill in blank
hardFill both blanks to unwrap optional email and provide a default value if nil.
iOS Swift
let email: String? = nil let userEmail = [1] ?? [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using force unwrap instead of nil-coalescing operator.
Providing nil as the default value.
✗ Incorrect
The nil-coalescing operator ?? unwraps the optional or uses the default value if nil.
5fill in blank
hardFill all three blanks to unwrap optional number safely and multiply by 2.
iOS Swift
let number: Int? = 10 if let [1] = [2] { let result = [3] * 2 print(result) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for unwrapped and optional variables.
Forcing unwrap inside if let instead of safe unwrapping.
✗ Incorrect
Use if let to unwrap number into safeNumber and then multiply.