Challenge - 5 Problems
Swift Optional Chaining Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this optional chaining example?
Consider the following Swift code. What will be printed when it runs?
Swift
class Person { var pet: Pet? } class Pet { var name: String init(name: String) { self.name = name } } let person = Person() if let petName = person.pet?.name { print(petName) } else { print("No pet name") }
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property of a nil optional using optional chaining.
✗ Incorrect
The person has no pet assigned, so person.pet is nil. Using optional chaining person.pet?.name returns nil safely, so the else branch runs and prints "No pet name".
❓ Predict Output
intermediate2:00remaining
What does this optional chaining expression return?
Given the code below, what is the value of 'length' after execution?
Swift
class Book { var title: String? } let book = Book() book.title = "Swift Guide" let length = book.title?.count print(length ?? "No length")
Attempts:
2 left
💡 Hint
Optional chaining returns an optional value even if the property is non-optional.
✗ Incorrect
book.title is a String? with value "Swift Guide". Using optional chaining book.title?.count returns an Int? with value 11 wrapped in Optional. Printing length shows Optional(11).
❓ Predict Output
advanced2:00remaining
What is the output of this nested optional chaining?
Analyze the code and determine what will be printed.
Swift
class Engine { var horsepower: Int init(horsepower: Int) { self.horsepower = horsepower } } class Car { var engine: Engine? } let car = Car() car.engine = Engine(horsepower: 150) if let hp = car.engine?.horsepower { print("Horsepower: \(hp)") } else { print("No engine horsepower") }
Attempts:
2 left
💡 Hint
Check if the engine property is nil or not before accessing horsepower.
✗ Incorrect
car.engine is assigned an Engine instance with horsepower 150. Optional chaining safely unwraps engine and accesses horsepower, printing "Horsepower: 150".
❓ Predict Output
advanced2:00remaining
What error does this code produce?
What happens when you run this Swift code?
Swift
class House { var address: String init(address: String) { self.address = address } } var house: House? house!.address = "123 Main St"
Attempts:
2 left
💡 Hint
What happens if you force unwrap a nil optional?
✗ Incorrect
house is nil. Using house! tries to force unwrap nil, causing a runtime crash with the message "Unexpectedly found nil while unwrapping an Optional value".
🧠 Conceptual
expert2:00remaining
How many items are in the resulting array after filtering with optional chaining?
Consider this Swift code snippet. How many elements does 'validLengths' contain after execution?
Swift
class Document { var text: String? init(text: String?) { self.text = text } } let docs = [Document(text: "Hello"), Document(text: nil), Document(text: "Swift"), Document(text: nil)] let validLengths = docs.compactMap { $0.text?.count }
Attempts:
2 left
💡 Hint
compactMap removes nil values after optional chaining.
✗ Incorrect
Only two Document instances have non-nil text: "Hello" (length 5) and "Swift" (length 5). compactMap collects these lengths, so validLengths contains 2 elements.