← All Swift Flashcard Decks

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
  1. You're designing an API for a payment processor. Multiple concrete types (`CreditCard`, `PayPal`, `ApplePay`) must all be usable wherever a `PaymentMethod` is expected. Which Swift mechanism is most appropriate?

    Answer: A protocol that all types conform to

    A `PaymentMethod` protocol defines the contract (e.g., `charge(amount:)`), and each concrete type independently conforms, enabling polymorphism without inheritance hierarchies.

  2. Your app has a `Logger` used across many modules. You want a single shared instance but still allow substituting a mock in tests. Which pattern works best in Swift?

    Answer: Dependency injection with a `LoggerProtocol` passed into each module

    Dependency injection through a protocol lets tests pass a `MockLogger` without changing production code, avoiding the testability trap of a bare singleton.

  3. A `Result` is returned from a fetch function. You need to map each post's title to uppercase on success. Which approach is most concise?

    Answer: `result.map { posts in posts.map { $0.title.uppercased() } }`

    `Result.map` transforms the success value without unwrapping, keeping the failure case untouched and producing a new `Result`.

  4. You want SwiftUI's `ProfileView` to automatically re-render when `user.name` changes. `User` is a reference type shared across views. What is the correct setup?

    Answer: Make `User` a class conforming to `ObservableObject` with `@Published var name`

    `ObservableObject` + `@Published` triggers SwiftUI's diffing engine; views holding `@ObservedObject` or `@StateObject` references re-render automatically when `name` changes.

  5. A team encounters a crash: `Fatal error: Unexpectedly found nil while unwrapping an Optional value`. You are asked to audit the codebase. Which code pattern most directly causes this crash?

    Answer: Force-unwrapping an optional with `!` when the value is nil

    The force-unwrap operator `!` crashes at runtime if the optional contains nil; replacing it with `if let`, `guard let`, or `??` eliminates this crash class.

  6. You need to sort an array of `Employee` structs by `salary` descending, then by `name` ascending when salaries are equal. Which Swift expression achieves this?

    Answer: `employees.sorted { $0.salary != $1.salary ? $0.salary > $1.salary : $0.name < $1.name }`

    A single comparator closure that checks salary first and falls back to name comparison when salaries are equal implements a stable multi-key sort.

  7. An iOS app uploads large files in the background. The upload must continue even if the user leaves the app. Which URLSession configuration is required?

    Answer: `URLSession(configuration: .background(withIdentifier: "com.app.upload"))`

    A background URLSession configuration hands off the upload to a system daemon, allowing it to continue after the app is suspended or terminated.

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