Adapter Pattern Implementation in Swift
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Adapter Pattern runs in O(1) time and O(1) space, in Swift as in every other language.
- ✓Protocol extensions let you adapt a type you do not own to a protocol you do own, entirely from outside both, extension LegacyClient: Notifier is legal and requires no access to the original source.
- ✓Write an extension conforming the adaptee directly to your protocol when the mapping is simple, and only introduce a wrapper struct when you need to hold extra state or when the adaptee would conform in two conflicting ways.
- ✓The Swift trap: Retroactive conformance is only safe when you own at least one of the two sides. If both the type and the protocol come from other modules, two different libraries can add conflicting conformances and the behaviour depends on link order, Swift now warns about exactly this, and the language's own evolution documents it as unsupported.
- ✓A call through a protocol existential goes via a witness table and cannot be inlined, and if the conforming value is larger than three words it is boxed on the heap.
What Adapter Pattern actually is
Adapter Pattern in Swift: what you actually reach for
Swift implementation
protocol Notifier {
func send(_ message: String)
}
// Default implementations keep conformers minimal.
extension Notifier {
func sendAll(_ messages: [String]) {
for message in messages { send(message) }
}
}
// Third-party type, unmodifiable.
struct LegacySmsClient {
func transmit(body: String, retries: Int = 3) {}
}
// RETROACTIVE CONFORMANCE: adapt a foreign type from outside, no wrapper.
extension LegacySmsClient: Notifier {
func send(_ message: String) {
transmit(body: message)
}
}
// Conditional conformance: an array of notifiers is itself a notifier.
extension Array: Notifier where Element: Notifier {
func send(_ message: String) {
for element in self { element.send(message) }
}
}
// Prefer generics or "some" over "any": the existential boxes and cannot inline.
func notify(_ notifier: some Notifier, _ message: String) {
notifier.send(message)
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Swift-specific trap
Performance in Swift, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Swift type or library should I use for Adapter Pattern?
Protocol extensions let you adapt a type you do not own to a protocol you do own, entirely from outside both, extension LegacyClient: Notifier is legal and requires no access to the original source. That capability, called retroactive conformance, makes Swift the language where this pattern needs the least machinery. Conditional conformance extends it to generics.
What is the most common Swift mistake when implementing Adapter Pattern?
Retroactive conformance is only safe when you own at least one of the two sides. If both the type and the protocol come from other modules, two different libraries can add conflicting conformances and the behaviour depends on link order, Swift now warns about exactly this, and the language's own evolution documents it as unsupported. The other trap is that a protocol with associated types or Self requirements cannot be used as a plain existential in older Swift versions, so the adapter must be generic rather than stored as a value of protocol type.
How does Adapter Pattern actually perform in Swift?
A call through a protocol existential goes via a witness table and cannot be inlined, and if the conforming value is larger than three words it is boxed on the heap. A generic function constrained to the protocol is specialised instead and costs nothing. So prefer some Notifier or a generic parameter over any Notifier when the adapter sits on a hot path. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Devin offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.