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 building a banking app where a `Transaction` must always have a non-zero `amount`. Which Swift feature best enforces this at the type level?
Answer: A failable initializer that returns nil for zero amounts
A failable initializer (`init?`) prevents creating an invalid `Transaction` by returning nil when the amount is zero, enforcing the invariant at construction time.
A networking layer returns `Data?` from a cache. You need to decode it as `User` or fall back to fetching from the server. Which pattern is most idiomatic in Swift?
Answer: Use `if let data = cachedData, let user = try? JSONDecoder().decode(User.self, from: data)`
`if let` chaining safely unwraps the optional data and attempts decoding without crashing or leaking errors into the call site.
Your app stores user settings with keys like `"theme"`, `"fontSize"`, etc. A teammate suggests using an enum with raw values instead of string literals. What is the primary benefit?
Answer: Compile-time safety prevents typos in key names
Enum cases with String raw values let the compiler catch misspelled key names, eliminating a class of runtime bugs common with bare string literals.
You have a `Document` class with an expensive `parse()` method. Many view controllers read `parsedContent` but parsing should happen only once. Which Swift pattern solves this?
Answer: A `lazy var` computed property that caches the result after first access
`lazy var` defers the expensive computation until first access and then stores the result, so subsequent reads are instant without manual caching logic.
A social media feed loads posts asynchronously. Which Swift concurrency construct lets you kick off loading user data and post images simultaneously, then wait for both before rendering?
Answer: Using `async let` for both fetches then awaiting them together
`async let` starts both async operations concurrently and the subsequent `await` on both ensures neither renders until both complete.
You need a `Stack` collection that works with any type and exposes `push`, `pop`, and `peek`. What is the correct Swift approach?
Answer: A generic struct with a private Array backing store
A generic struct `Stack` with a private `[T]` backing store is idiomatic Swift: value semantics, type-safe, and zero boxing overhead.
A `UITableView` delegate method calls a closure stored in a cell's view model. The closure captures `self` (the view controller). What memory issue can arise and how do you fix it?
Answer: A retain cycle keeping the view controller alive; capture `self` as `weak` or `unowned`
If the view model's closure strongly captures the view controller, and the view controller owns the view model, a retain cycle forms — breaking it requires `[weak self]` or `[unowned self]`.
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