Protocol composition lets you combine multiple protocols into one requirement. It means a type must conform to all listed protocols.
Click to reveal answer
beginner
How do you write a protocol composition type in Swift?
Use the ampersand (&) symbol between protocols, like ProtocolA & ProtocolB.
Click to reveal answer
intermediate
Why use protocol composition instead of a single protocol?
It allows flexibility by combining existing protocols without creating a new one. You can require multiple behaviors at once.
Click to reveal answer
beginner
Example: What does this mean? func performAction(with object: ProtocolA & ProtocolB)
The function accepts an object that conforms to both ProtocolA and ProtocolB. The object must implement all requirements from both.
Click to reveal answer
intermediate
Can protocol composition be used with class and protocol types?
Yes. You can combine protocols and also require a class type by adding <code>AnyObject</code> in the composition, like <code>ProtocolA & AnyObject</code>.
Click to reveal answer
How do you combine two protocols, ProtocolA and ProtocolB, in Swift?
AProtocolA & ProtocolB
BProtocolA | ProtocolB
CProtocolA + ProtocolB
DProtocolA, ProtocolB
✗ Incorrect
Use the ampersand (&) to combine protocols in Swift.
What does a function parameter typed as ProtocolA & ProtocolB require?
AAn object conforming to ProtocolA only
BAn object conforming to either ProtocolA or ProtocolB
CAn object conforming to both ProtocolA and ProtocolB
DAn object conforming to ProtocolB only
✗ Incorrect
The object must conform to all protocols in the composition.
Can protocol composition include class type constraints?
AYes, by including AnyObject in the composition
BNo, only protocols can be composed
CYes, by using class keyword inside composition
DNo, class types are separate
✗ Incorrect
Including AnyObject restricts the composition to class types.
Why might you use protocol composition instead of defining a new protocol?
ATo inherit from multiple classes
BTo create a new class
CTo override protocol methods
DTo avoid code duplication and combine existing protocols
✗ Incorrect
Protocol composition combines existing protocols without creating a new one.
Which of these is NOT a valid protocol composition in Swift?
AProtocolA & AnyObject
BProtocolA | ProtocolB
CProtocolA & ProtocolB
DProtocolA & ProtocolB & AnyObject
✗ Incorrect
The | symbol is not used for protocol composition in Swift.
Explain what protocol composition is and how it is used in Swift.
Think about how you can require multiple behaviors at once.
You got /3 concepts.
Describe a situation where protocol composition is better than creating a new protocol.
Consider when you want to require multiple capabilities without new names.
You got /3 concepts.
Practice
(1/5)
1. What does protocol composition in Swift allow you to do?
easy
A. Override methods from multiple protocols automatically
B. Create a new class that inherits from multiple classes
C. Define a protocol that inherits from a single class
D. Combine multiple protocols into one requirement using &
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 D
Quick Check:
Protocol composition = Combine protocols with & [OK]
Hint: Remember & joins protocols to require multiple behaviors [OK]
Common Mistakes:
Confusing protocol composition with class inheritance
Thinking protocols can inherit from classes
Assuming protocol composition overrides methods
2. Which of the following is the correct syntax for protocol composition in Swift?
easy
A. protocol Combined = ProtocolA & ProtocolB
B. let combined: ProtocolA & ProtocolB
C. protocol Combined: ProtocolA & ProtocolB {}
D. class Combined: ProtocolA & ProtocolB {}
Solution
Step 1: Identify correct usage of protocol composition
Protocol composition is used in type annotations like let 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 B
Quick Check:
Protocol composition syntax = type annotation with & [OK]
Hint: Use & in type declarations, not in protocol inheritance [OK]
Common Mistakes:
Trying to inherit protocols with & in protocol declaration
Using = instead of : or &
Using class inheritance syntax with protocols
3. What will be the output of this Swift code?
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)
medium
A. bar
B. foo
C. foo
bar
D. Compilation error
Solution
Step 1: Understand protocol composition parameter
The function test requires 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 calling foo() and bar() prints "foo" and "bar" respectively.
Final Answer:
foo
bar -> Option C
Quick Check:
Both protocol methods called = foo and bar printed [OK]
Hint: Both protocols' methods run when composed type passed [OK]
Common Mistakes:
Assuming only one protocol's method runs
Thinking protocol composition causes errors
Confusing struct conformance with protocol composition
4. Identify the error in this Swift code using protocol composition:
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.