Optional chaining is a way to safely access properties, methods, and subscripts on an optional that might currently be nil. It uses the ? symbol to check if the optional has a value before accessing it.
Click to reveal answer
beginner
How does optional chaining differ from forced unwrapping?
Optional chaining safely returns nil if the optional is nil, without causing a runtime error. Forced unwrapping (!) crashes the program if the optional is nil.
Click to reveal answer
intermediate
What does the expression 'person?.address?.street' mean in Swift?
It means: if 'person' is not nil, then access 'address'; if 'address' is not nil, then access 'street'. If any part is nil, the whole expression returns nil safely.
Click to reveal answer
intermediate
Can optional chaining be used to call methods? Give an example.
Yes. For example, 'person?.printName()' calls 'printName()' only if 'person' is not nil. If 'person' is nil, the call is ignored and returns nil.
Click to reveal answer
intermediate
What is the return type of an optional chaining expression?
The return type is always an optional, even if the property or method normally returns a non-optional value. This is because the chain might fail and return nil.
Click to reveal answer
What happens if you use optional chaining on a nil value?
AThe program crashes with a runtime error.
BThe compiler throws an error.
CThe optional is automatically unwrapped.
DThe expression returns nil safely without crashing.
✗ Incorrect
Optional chaining returns nil safely if any part of the chain is nil, preventing crashes.
Which symbol is used for optional chaining in Swift?
A?
B!
C.
D->
✗ Incorrect
The '?' symbol is used after an optional to safely access its properties or methods.
What is the type of the result when you use optional chaining to access a property?
AAlways String
BNon-optional property type
COptional of the property's type
DAlways Int
✗ Incorrect
Optional chaining returns an optional because the property might not be accessible if the chain fails.
Can optional chaining be used to call methods that return Void?
ANo, methods returning Void cannot be called with optional chaining.
BYes, it returns Void? (optional Void).
CYes, but it returns a Bool.
DOnly if the method is static.
✗ Incorrect
Optional chaining can call methods returning Void, and the result is an optional Void (Void?).
What is the difference between optional chaining and forced unwrapping?
AOptional chaining safely returns nil if nil, forced unwrapping crashes if nil.
BOptional chaining crashes if nil, forced unwrapping returns nil.
CBoth crash if nil.
DBoth return nil if nil.
✗ Incorrect
Optional chaining safely returns nil without crashing, while forced unwrapping crashes if the optional is nil.
Explain how optional chaining works in Swift and why it is useful.
Think about how you check if something exists before using it.
You got /4 concepts.
Describe the difference between optional chaining and forced unwrapping with examples.
Consider what happens when the optional is nil in each case.
You got /3 concepts.
Practice
(1/5)
1. What does the ?. operator do in Swift when used with optionals?
easy
A. It forces unwraps the optional and crashes if nil.
B. It converts the optional to a non-optional value automatically.
C. It safely accesses a property or method and returns nil if the optional is nil.
D. It assigns a default value if the optional is nil.
Solution
Step 1: Understand optional chaining purpose
The ?. operator is used to safely access properties or methods on an optional value.
Step 2: Behavior when optional is nil
If the optional is nil, the chaining stops and returns nil instead of crashing.
Final Answer:
It safely accesses a property or method and returns nil if the optional is nil. -> Option C
Quick Check:
Optional chaining = safe access with nil return [OK]
Hint: Remember: ? stops if nil, no crash happens [OK]
Common Mistakes:
Confusing optional chaining with forced unwrapping
Thinking it unwraps optionals automatically
Assuming it provides default values
2. Which of the following is the correct syntax to safely access the name property of an optional person object using optional chaining?
easy
A. person->name
B. person!.name
C. person.name?
D. person?.name
Solution
Step 1: Identify correct optional chaining syntax
Optional chaining uses ?. between the optional variable and the property.
Step 2: Check each option
person?.name uses person?.name, which is correct syntax. person!.name forces unwraps with !. person.name? places ? incorrectly. person->name uses invalid operator.
Hint: Use ?. right before the property to chain safely [OK]
Common Mistakes:
Using ! instead of ? for optional chaining
Placing ? after the property name
Using invalid operators like ->
3. Consider the following Swift code:
class Person {
var pet: Pet?
}
class Pet {
var name: String
init(name: String) { self.name = name }
}
let person = Person()
let petName = person.pet?.name
print(petName)
What will be printed?
medium
A. Optional(nil)
B. nil
C. An empty string
D. Runtime error
Solution
Step 1: Analyze the optional chaining result
Since person.pet is nil (not assigned), person.pet?.name returns nil of type String?.
Step 2: Understand print output of optional nil
Printing an optional value that is nil in Swift shows Optional(nil) because the variable itself is an optional.
Final Answer:
Optional(nil) -> Option A
Quick Check:
print(String? nil) = Optional(nil) [OK]
Hint: Printing optional nil shows 'Optional(nil)', not 'nil' [OK]
Common Mistakes:
Thinking print shows nil
Expecting empty string output
Thinking it causes runtime error
4. The following Swift code causes a compile error. What is the mistake?
struct Car {
var owner: Person?
}
let car = Car()
let ownerName = car.owner?.name!
medium
A. Using forced unwrap (!) after optional chaining without parentheses
B. Missing initialization of car variable
C. Incorrect use of optional chaining operator
D. Person type does not have a name property
Solution
Step 1: Identify forced unwrap misuse
The code uses car.owner?.name! which tries to force unwrap the property without parentheses.
Step 2: Correct syntax for forced unwrap after optional chaining
Forced unwrap must be applied to the entire optional chaining expression with parentheses like (car.owner?.name)!.
Final Answer:
Using forced unwrap (!) after optional chaining without parentheses -> Option A
Quick Check:
Force unwrap after optional chaining needs parentheses [OK]
Hint: Wrap optional chain in () before ! to force unwrap [OK]
Common Mistakes:
Forgetting parentheses around optional chain before !
Assuming optional chaining can be forced unwrapped directly
Ignoring compiler error details
5. Given the following classes:
class Address {
var city: String?
}
class User {
var address: Address?
}
let user = User()
Which code safely prints the city name if it exists, or "Unknown" if city or address is nil?
hard
A. print(user.address.city ?? "Unknown")
B. print(user.address?.city ?? "Unknown")
C. print(user.address!.city!)
D. print(user.address?.city!)
Solution
Step 1: Understand optional chaining with nil coalescing
Using user.address?.city safely accesses city, returning nil if address or city is nil.
Step 2: Use nil coalescing operator to provide default
The ?? "Unknown" provides "Unknown" if the optional is nil, avoiding crashes.
Final Answer:
print(user.address?.city ?? "Unknown") -> Option B
Quick Check:
Optional chaining + ?? = safe access with default [OK]
Hint: Combine ?. with ?? to provide default value safely [OK]
Common Mistakes:
Forcing unwraps causing runtime crashes
Not handling nil for nested optionals
Using optional chaining without default fallback
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.