Protocol composition lets you combine multiple protocols into one requirement, so a type must conform to all of them at once.
Click to reveal answer
beginner
How do you write a protocol composition type in Swift?
Use the syntax: ProtocolA & ProtocolB to require a type that conforms to both ProtocolA and ProtocolB.
Click to reveal answer
intermediate
Why use protocol composition instead of inheritance?
Protocol composition allows combining behaviors from multiple protocols without needing a class hierarchy, making code more flexible and reusable.
Click to reveal answer
beginner
Example: What does this mean? func performAction(on object: Drawable & Animatable)
The function requires an object that conforms to both Drawable and Animatable protocols, so it can draw and animate the object.
Click to reveal answer
intermediate
Can protocol composition be used with class-only protocols?
Yes, you can combine class-only protocols using protocol composition, and the resulting type will also be class-constrained.
Click to reveal answer
Which syntax correctly represents protocol composition in Swift?
AProtocolA + ProtocolB
BProtocolA | ProtocolB
CProtocolA, ProtocolB
DProtocolA & ProtocolB
✗ Incorrect
Protocol composition uses the & symbol to combine protocols.
What does a function parameter typed as Readable & Writable require?
AAn object that is both Readable and Writable
BAn object that is only Readable
CAn object that is neither Readable nor Writable
DAn object that is either Readable or Writable
✗ Incorrect
The & means the object must conform to both protocols.
Can protocol composition be used with structs in Swift?
ANo, only classes can use protocol composition
BOnly enums can use protocol composition
CYes, any type can conform to multiple protocols
DOnly protocols can use protocol composition
✗ Incorrect
Structs can conform to multiple protocols and be used with protocol composition.
What is a benefit of using protocol composition?
AIt allows combining multiple behaviors flexibly
BIt forces inheritance from a single class
CIt restricts types to only one protocol
DIt replaces all protocols with classes
✗ Incorrect
Protocol composition lets you combine multiple protocols for flexible design.
If a protocol is class-only, what happens when you compose it with another protocol?
AThe composition is no longer class-only
BThe composition remains class-only
CThe composition becomes a struct
DThe composition is invalid
✗ Incorrect
Class-only constraints carry over in protocol composition.
Explain how protocol composition works in Swift and give a simple example.
Think about how you can require multiple behaviors at once.
You got /3 concepts.
Describe a real-life scenario where protocol composition would be useful in Swift programming.
Imagine an object that needs to do more than one thing.
You got /3 concepts.
Practice
(1/5)
1. What does protocol composition in Swift allow you to do?
easy
A. Combine multiple protocols into one requirement without creating a new protocol
B. Create a new class that inherits from multiple classes
C. Define a protocol with stored properties
D. Override methods in a protocol extension
Solution
Step 1: Understand protocol composition concept
Protocol composition uses the & operator to require multiple protocols at once without defining a new protocol.
Step 2: Compare options with concept
Only Combine multiple protocols into one requirement without creating a new protocol correctly describes combining multiple protocols without creating a new one. Other options describe unrelated or incorrect Swift features.
Final Answer:
Combine multiple protocols into one requirement without creating a new protocol -> Option A
Quick Check:
Protocol composition = Combine protocols [OK]
Hint: Remember & means combine protocols, not inherit classes [OK]
Common Mistakes:
Confusing protocol composition with class inheritance
Thinking protocols can have stored properties
Believing protocol extensions override methods
2. Which of the following is the correct syntax to declare a function parameter that requires both Codable and Equatable protocols using protocol composition?
easy
A. func save(item: Codable | Equatable) {}
B. func save(item: Codable & Equatable) {}
C. func save(item: Codable, Equatable) {}
D. func save(item: Codable && Equatable) {}
Solution
Step 1: Recall protocol composition syntax
Swift uses & to combine protocols in type requirements, like Codable & Equatable.
Step 2: Evaluate each option
func save(item: Codable & Equatable) {} uses correct & syntax. func save(item: Codable | Equatable) {} uses | which is invalid. func save(item: Codable, Equatable) {} uses comma which is invalid. func save(item: Codable && Equatable) {} uses && which is invalid.
Final Answer:
func save(item: Codable & Equatable) {} -> Option B
Quick Check:
Use & to combine protocols [OK]
Hint: Use & to combine protocols, not |, comma, or && [OK]
Common Mistakes:
Using | or && instead of & for protocol composition
Separating protocols with commas in parameter types
Confusing logical operators with protocol composition
3. What will be the output of the following Swift code?
protocol A { func foo() }
protocol B { func bar() }
struct MyStruct: A, B {
func foo() { print("foo") }
func bar() { print("bar") }
}
func callBoth(item: A & B) {
item.foo()
item.bar()
}
let obj = MyStruct()
callBoth(item: obj)
medium
A. foo\nbar
B. foo
C. bar
D. Compilation error
Solution
Step 1: Understand protocol composition usage
The function callBoth requires an argument conforming to both A and B. MyStruct conforms to both.
Step 2: Trace function calls
Inside callBoth, foo() prints "foo" and bar() prints "bar". Both are called in order.
Final Answer:
foo
bar -> Option A
Quick Check:
Both protocol methods called = foo\nbar [OK]
Hint: Both protocols' methods run in order when composed [OK]
Common Mistakes:
Assuming only one protocol method runs
Expecting a compile error due to protocol composition
Confusing protocol conformance with inheritance
4. Identify the error in this Swift code using protocol composition:
Struct S conforms only to P1, missing P2 conformance.
Step 2: Analyze function parameter requirement
Function testFunc requires a parameter conforming to both P1 and P2. Passing s fails because s lacks P2.
Final Answer:
Struct S does not conform to protocol P2 -> Option D
Quick Check:
Missing protocol conformance causes error [OK]
Hint: Check all protocols are conformed before passing composed type [OK]
Common Mistakes:
Assuming partial protocol conformance is enough
Thinking function parameter syntax is incorrect
Believing protocols cannot be combined with &
5. Given these protocols and structs:
protocol Drawable { func draw() }
protocol Colorable { var color: String { get } }
struct Circle: Drawable, Colorable {
var color: String
func draw() { print("Drawing a circle") }
}
struct Square: Drawable {
func draw() { print("Drawing a square") }
}
Which of the following function signatures correctly accepts only objects that are both Drawable and Colorable?
hard
A. func render(item: Drawable) { item.draw() }
B. func render(item: Drawable | Colorable) { item.draw() }
C. func render(item: Drawable & Colorable) { item.draw(); print(item.color) }
D. func render(item: Colorable) { print(item.color) }
Solution
Step 1: Understand protocol composition for parameters
To accept only objects conforming to both Drawable and Colorable, use Drawable & Colorable.
Step 2: Check each function signature
func render(item: Drawable & Colorable) { item.draw(); print(item.color) } uses correct composition and accesses both draw() and color. Options A, B, and D either use invalid syntax or accept only one protocol.