Top 10 Swift scenario questions
Try each one yourself first, then open the model answer.
1. The app crashes with 'unexpectedly found nil'. How do you prevent it?
Show answerHide answer
Avoid force unwrapping. Use if let, guard let or the nil coalescing operator, and handle the missing case deliberately.
2. A view controller is never deallocated. What would you check?
Show answerHide answer
Look for a retain cycle, often a closure capturing self strongly or a strong delegate. Use weak or unowned captures and weak delegate references.
3. The UI does not update after a network response. Why might that be?
Show answerHide answer
UI work must happen on the main thread. Mark UI-updating code with MainActor or dispatch to the main queue.
4. A change to a copied struct does not affect the original. Is that a bug?
Show answerHide answer
No. Structs have value semantics. Use a class if you need shared identity.
5. Several tasks update the same data and results are inconsistent. What would you use?
Show answerHide answer
An actor, which serialises access to its mutable state. Remember that state can change across await points.
6. You need to load three resources at once and wait for all. How?
Show answerHide answer
Use async let for a fixed number, or a task group for a dynamic number.
7. A user leaves a screen while a download is running. How do you stop it?
Show answerHide answer
Hold the Task and cancel it, or use SwiftUI's task modifier, and check for cancellation in long work.
8. A SwiftUI view resets its state unexpectedly. What is a common reason?
Show answerHide answer
The view's identity changed or an owned object was created with the wrong property wrapper. Use State or StateObject for data the view owns.
9. JSON fields use snake_case and decoding fails. What do you do?
Show answerHide answer
Set the decoder's keyDecodingStrategy to convertFromSnakeCase or define CodingKeys, and handle optional fields.
10. Where should an access token be stored on the device?
Show answerHide answer
In the Keychain, not in UserDefaults or source code.
