How do protocols and protocol extensions enable protocol-oriented programming?
Assesses fundamental understanding of Swift conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
A protocol declares requirements such as properties, methods, initialisers or associated types that conforming types implement. Swift encourages protocol-oriented programming: define behaviour once in extensions and compose protocols instead of building deep inheritance trees.
protocol Shape {
var area: Double { get }
}
extension Shape {
var description: String { "area: \(area)" } // default implementation
}
struct Circle: Shape {
let r: Double
var area: Double { .pi * r * r }
}
Extensions can add default methods, computed properties and conformance to other protocols. Protocols with associated types cannot be used as existential types before Swift 5.7's any; previously you used generics or type erasure.
Protocols support inheritance and composition such as some Shape & Codable. Prefer them for testability, dependency injection and sharing behaviour across unrelated types.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.