📖 Scenario: Imagine you are building a simple app that manages different types of users. Some users can log in, and some users can also post messages. You want to create a way to group these capabilities together using protocols.
🎯 Goal: You will create two protocols, then use protocol composition to define a type that requires both capabilities. Finally, you will create a user that conforms to both protocols and print a message showing the combined abilities.
📋 What You'll Learn
Create a protocol named Loginable with a method login() that prints "User logged in"
Create a protocol named Postable with a method postMessage() that prints "Message posted"
Create a type alias named UserActions that composes Loginable and Postable
Create a struct named User that conforms to UserActions and implements both methods
Create an instance of User and call both login() and postMessage()
Print the outputs exactly as specified
💡 Why This Matters
🌍 Real World
Protocol composition helps organize code by grouping multiple capabilities together, making it easier to write flexible and reusable code.
💼 Career
Understanding protocol composition is important for Swift developers to design clean, modular, and maintainable codebases, especially in app development.
Progress0 / 4 steps
1
Create the protocols
Create a protocol called Loginable with a method login() that prints "User logged in". Also create a protocol called Postable with a method postMessage() that prints "Message posted".
Swift
Hint
Protocols define methods without implementation. Just declare the method signatures.
2
Create the protocol composition type alias
Create a type alias called UserActions that composes the protocols Loginable and Postable using protocol composition syntax.
Swift
Hint
Use typealias UserActions = Loginable & Postable to combine protocols.
3
Create a struct conforming to the composed protocols
Create a struct called User that conforms to UserActions. Implement the login() method to print "User logged in" and the postMessage() method to print "Message posted".
Swift
Hint
Implement both methods inside the struct with the exact print statements.
4
Create an instance and call the methods
Create an instance of User named user. Call user.login() and user.postMessage() to print the messages.
Swift
Hint
Make sure to create the instance and call both methods exactly as shown.
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.