What if you could ask for multiple skills at once without messy checks?
Why Protocol composition in Swift? - Purpose & Use Cases
Imagine you have several roles in a team, like a designer and a developer. You want to assign tasks only to people who can do both jobs. Without a clear way to combine these roles, you have to check each person manually every time.
Manually checking if someone fits multiple roles is slow and error-prone. You might forget a check or write repetitive code for each combination, making your program messy and hard to maintain.
Protocol composition lets you combine multiple protocols into one requirement. This way, you can easily say "I need someone who is both a designer and a developer" without writing extra checks. It keeps your code clean and clear.
func assignTask(person: Designer) {
if let dev = person as? Developer {
// assign task
}
}func assignTask(person: Designer & Developer) {
// assign task directly
}It enables writing concise, readable code that clearly expresses combined capabilities without extra checks.
In an app, you might want to update UI elements only if the object can both display and animate. Protocol composition lets you require both abilities at once, making your code safer and simpler.
Manually combining roles is slow and error-prone.
Protocol composition combines multiple protocols into one requirement.
This leads to cleaner, easier-to-read code that expresses combined abilities clearly.
Practice
protocol composition in Swift allow you to do?Solution
Step 1: Understand protocol composition concept
Protocol composition uses the & symbol to combine multiple protocols into one requirement.Step 2: Differentiate from class inheritance
Swift does not support multiple class inheritance, but protocol composition allows combining multiple protocols.Final Answer:
Combine multiple protocols into one requirement using & -> Option DQuick Check:
Protocol composition = Combine protocols with & [OK]
- Confusing protocol composition with class inheritance
- Thinking protocols can inherit from classes
- Assuming protocol composition overrides methods
Solution
Step 1: Identify correct usage of protocol composition
Protocol composition is used in type annotations likelet variable: ProtocolA & ProtocolB.Step 2: Check other options for syntax errors
Options B and C misuse protocol inheritance or assignment syntax; D tries class inheritance from protocols incorrectly.Final Answer:
let combined: ProtocolA & ProtocolB -> Option BQuick Check:
Protocol composition syntax = type annotation with & [OK]
- Trying to inherit protocols with & in protocol declaration
- Using = instead of : or &
- Using class inheritance syntax with protocols
protocol A { func foo() }
protocol B { func bar() }
struct S: A, B {
func foo() { print("foo") }
func bar() { print("bar") }
}
func test(x: A & B) {
x.foo()
x.bar()
}
let s = S()
test(x: s)Solution
Step 1: Understand protocol composition parameter
The functiontestrequires an argument conforming to both protocols A and B.Step 2: Check struct S conformance and method calls
S conforms to both A and B, so callingfoo()andbar()prints "foo" and "bar" respectively.Final Answer:
foo bar -> Option CQuick Check:
Both protocol methods called = foo and bar printed [OK]
- Assuming only one protocol's method runs
- Thinking protocol composition causes errors
- Confusing struct conformance with protocol composition
protocol P1 { func p1() }
protocol P2 { func p2() }
func doSomething(x: P1 & P2) {
x.p1()
x.p2()
}
struct MyStruct: P1 {
func p1() { print("p1") }
}
doSomething(x: MyStruct())Solution
Step 1: Check struct conformance to protocols
MyStruct conforms only to P1, missing P2 conformance.Step 2: Understand function parameter requirement
doSomething requires an argument conforming to both P1 and P2, so MyStruct instance is invalid.Final Answer:
MyStruct does not conform to P2, so cannot be passed to doSomething -> Option AQuick Check:
Protocol composition requires all protocols conformed [OK]
- Passing instance conforming to only one protocol
- Thinking protocol composition allows partial conformance
- Ignoring compiler errors about missing protocol methods
protocol Drawable { func draw() }
protocol Colorful { var color: String { get } }
struct Circle: Drawable, Colorful {
var color: String
func draw() { print("Drawing a \(color) circle") }
}
struct Square: Drawable {
func draw() { print("Drawing a square") }
}Which code snippet correctly uses protocol composition to accept only Drawable and Colorful types?
Solution
Step 1: Identify correct protocol composition syntax
func render(item: Drawable & Colorful) { item.draw() } usesDrawable & Colorfulcorrectly in parameter type.Step 2: Check other options for syntax or logic errors
func render(item: Drawable | Colorful) { item.draw() } uses | which is invalid; C misuses where clause syntax; D uses where clause incorrectly on parameter.Final Answer:
func render(item: Drawable & Colorful) { item.draw() } -> Option AQuick Check:
Use & to combine protocols in function parameters [OK]
- Using | instead of & for protocol composition
- Misusing where clauses in parameter declarations
- Trying to filter protocol properties in parameter signature
Ready to prove it?
Pass the test and this pattern moves unknown
