Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to safely access the street name using optional chaining.
Swift
let streetName = person.address[1]street Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' instead of '?.' causes a compile error if the optional is nil.
Using '!' forces unwrapping and can cause runtime crashes.
✗ Incorrect
Using '?.' allows safe access to street property if address is not nil.
2fill in blank
mediumComplete the code to call the optional method safely using optional chaining.
Swift
let result = person.delegate[1]performAction() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' causes compile error if delegate is nil.
Using '!' can cause runtime crash if delegate is nil.
✗ Incorrect
Optional chaining with '?.' safely calls the method if delegate is not nil.
3fill in blank
hardFix the error in safely accessing the nested optional property.
Swift
let zipCode = person.address[1]postalCode[2]code
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' for the first optional causes compile error.
Using '!' can cause runtime crash if optional is nil.
✗ Incorrect
Use optional chaining for the first optional, then normal dot for non-optional property.
4fill in blank
hardFill both blanks to safely access the city name from an optional nested property.
Swift
let city = person[1]address[2]city
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.' for the optional property causes errors.
Using '!' risks runtime crashes if the optional is nil.
✗ Incorrect
Use optional chaining for the first optional property, then dot for the non-optional city.
5fill in blank
hardFill all three blanks to safely call a method on an optional delegate's optional property.
Swift
let value = person[1]delegate[2]optionalProperty[3]computeValue()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!' can cause runtime crashes if any optional is nil.
Using '.' for optional properties causes compile errors.
✗ Incorrect
Use optional chaining for optional delegate and optionalProperty, dot for non-optional property.