SOLID Principles

The interviewer asks

“Explain the SOLID principles. How do they apply to iOS?”

How to answer

SOLID is five design guidelines for code that's easy to change and test. Single responsibility: a type has one reason to change, so a view model doesn't also parse JSON and cache images. Open/closed: add behavior by adding types (a new protocol conformance, a new enum case handled in one place) rather than editing working code. Liskov substitution: any conformer must honor the protocol's contract, including errors and isolation, so a fake really can stand in for the real service. Interface segregation: small, role-based protocols so clients and fakes depend only on what they use. Dependency inversion: high-level policy depends on abstractions it owns, with implementations injected through init. In Swift these map onto protocols, value types, composition and Sendable service boundaries more than onto class inheritance.

Key points

  • SRP: split by reason to change (UI state vs data access vs formatting), not by line count
  • OCP: protocols and generics for extension points; switch over a closed enum is fine when the set really is closed
  • LSP: a conformer must not strengthen preconditions or throw unexpected errors; UIKit subclass overrides that skip super break it
  • ISP: UserFetching and UserSaving instead of a twelve-method UserServiceProtocol
  • DIP: the core defines protocol OrderStore: Sendable; the network module implements it

If they dig deeper

  • Swift-specific: protocol extensions and default implementations help OCP; value types sidestep many LSP problems that come from inheritance; some/any let you choose static or dynamic substitution
  • Concurrency: LSP now includes isolation. A protocol requirement that's nonisolated can't be satisfied by a main-actor method unless you use an isolated conformance (Swift 6.2), and a Sendable protocol requires thread-safe conformers
  • Pragmatism: SOLID is a means to testability and change; over-applying it creates a protocol and three files per class

Common pitfalls

  • Reciting the acronym without one concrete iOS example per letter
  • SRP taken to one-method classes, which spreads one feature across twenty files
  • "Dependency inversion" that's really a protocol mirroring one concrete class
  • Massive view controllers or view models presented as "it's MVVM, so it's SOLID"

How to use it

swift
struct Receipt: Sendable { let total: Decimal }

// ISP: small, role-based protocols
protocol ReceiptFormatting: Sendable { func text(for receipt: Receipt) -> String }
protocol ReceiptSending: Sendable { func send(_ text: String, to email: String) async throws }

// OCP: new formats are new types; existing code isn't edited
struct PlainTextFormatter: ReceiptFormatting {
    func text(for receipt: Receipt) -> String {
        "Total: \(receipt.total.formatted(.currency(code: "EUR")))"
    }
}

// LSP: any ReceiptSending (real or fake) must behave the same from the caller's view
actor RecordingSender: ReceiptSending {
    private(set) var sent: [String] = []
    func send(_ text: String, to email: String) async throws { sent.append(text) }
}

// SRP + DIP: orchestrates only; depends on abstractions injected through init
struct EmailReceiptUseCase: Sendable {
    let formatter: any ReceiptFormatting
    let sender: any ReceiptSending

    func execute(_ receipt: Receipt, email: String) async throws {
        try await sender.send(formatter.text(for: receipt), to: email)
    }
}

Best practices

  • Give one concrete iOS example per principle in an interview
  • Split types by reason to change, and stop when a split no longer helps tests or readability
  • Define protocols from the consumer side and keep them small
  • Inject dependencies through init; keep Sendable at concurrency boundaries
  • Prefer composition and protocols over subclassing

Related concepts

  • Protocol-oriented architecture, dependency injection, hexagonal architecture, clean architecture, use case, MVC