Recall & Review
beginner
What is optional chaining in Swift?
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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?
✗ 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.