Case Studies & Practical Application Flashcards
7 cards from real Swift practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Case Studies & Practical Application flashcards as text
You're refactoring a legacy codebase where many functions return `-1` to signal errors. Which modern Swift pattern replaces this anti-pattern?
Answer: Returning `Int?` with nil for errors, or throwing a typed Error
Optional returns (`Int?`) signal absence without sentinel values, and throwing functions with typed errors give callers structured error information — both are idiomatic replacements for magic-number conventions.
A `ProductListViewModel` fetches data and your unit test must verify that `products` is populated after a successful network call. You mock the network layer. What is the key design requirement for this to work?
Answer: The ViewModel must depend on a protocol, not a concrete network class
If the ViewModel holds a protocol reference (e.g., `NetworkServiceProtocol`), tests can inject a `MockNetworkService` that returns canned data without real HTTP calls.
Your Swift app parses untrusted JSON where a field might be a `String` or an `Int`. Using `Codable`, how do you handle this ambiguity?
Answer: Write a custom `init(from:)` that tries decoding as String, then falls back to Int
A custom `init(from decoder:)` can attempt `container.decode(String.self, ...)` and catch the error to retry with `Int.self`, handling the type ambiguity gracefully.
You have a Swift actor `DatabaseActor` with a method `func fetchUser(id: Int) async -> User`. On which thread does the body of `fetchUser` execute?
Answer: On the actor's internal serial executor, isolated from other concurrent calls
Actors serialize access through their own executor, so `fetchUser`'s body never runs concurrently with other methods on the same actor, preventing data races.
A colleague writes `var items: [Item] = []` as a property of a SwiftUI `View` struct to hold fetched data. Why won't changes to `items` cause the view to re-render?
Answer: Plain `var` properties don't participate in SwiftUI's state management; `@State` is needed
SwiftUI only re-renders a view when a property wrapped with `@State`, `@ObservedObject`, or similar triggers a change notification — plain `var` assignments are invisible to the framework.
You need to ensure a `FileManager` operation runs on a background thread and its result is delivered on the main thread in Swift Concurrency. What is the correct structure?
Answer: Run the operation in a `Task.detached` block or background task, then use `await MainActor.run { ... }` for UI updates
Performing I/O off the main thread then calling `await MainActor.run` (or switching to a `@MainActor` context) is the Swift Concurrency equivalent of the classic GCD background-then-main pattern.
A `CartViewModel` holds a `[CartItem]` and multiple views observe it. When `CartItem` is a struct, what happens to view state when you call `cart.items.remove(at: 0)`?
Answer: All views observing the `@Published items` property are notified and re-render
Mutating a `@Published` array property (even with struct elements) publishes the change through Combine, notifying every subscriber regardless of which view initiated the mutation.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy