Explain it simply
Protocols, in plain language
Protocol is a contract that says what properties/methods a class, struct, or enum must have. Protocol-oriented programming is used heavily in Swift development.
Problem → Why → Solution
The problem — why this concept matters
If you miss writing one of the methods/properties a protocol requires, the type won't conform and you'll get a compile error.
A common mistake
Assuming one sample output proves nil, ownership, actor isolation, and every error path.
Core concept — understand how it works
Use Protocols to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
- value or reference
- protocol and type composition
- reusable behavior
Explain the type, ownership, and runtime behavior of Protocols
Core syntax or pattern
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()Use Protocols to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
Read the code step by step
- value or reference
protocol Payable {Protocol is a contract that says what properties/methods a class, struct, or enum must have. Protocol-oriented programming is used heavily in Swift development.
- protocol and type composition
func pay() {Use Protocols to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
- reusable behavior
payment.pay()Run and test an original Protocols example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- value or reference
- protocol and type composition
- reusable behavior
Success
Explain the type, ownership, and runtime behavior of Protocols
Failure
If you miss writing one of the methods/properties a protocol requires, the type won't conform and you'll get a compile error.
Protocol is a contract that says what properties/methods a class, struct, or enum must have. Protocol-oriented programming is used heavily in Swift development.
Using protocols lets you write code based on behavior instead of being tied to one concrete type. They're extremely useful for testing, dependency injection, and reusable components.
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()Payable protocol requires an amount property and a pay() method. CreditCardPayment struct follows that contract.
Paid $29.99 with credit cardReal-world usage
How to apply it in a real project
Run and test an original Protocols example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Mini challenge
Build it yourself
Run and test an original Protocols example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
protocol Payable {
var amount: Double { get }
func pay()
}
struct CreditCardPayment: Payable {
let amount: Double
func pay() {
print("Paid $\(amount) with credit card")
}
}
let payment = CreditCardPayment(amount: 29.99)
payment.pay()Expected: Explain the type, ownership, and runtime behavior of Protocols
Show hint
Run and test an original Protocols example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- value or reference → protocol and type composition → reusable behavior
- Use Protocols to express stable capabilities and reusable algorithms. Prefer protocol-oriented design, constrained generics, and focused extensions to reduce coupling to concrete types, inheritance, and global state.
- If you miss writing one of the methods/properties a protocol requires, the type won't conform and you'll get a compile error.
- Run and test an original Protocols example with empty, nil, boundary, invalid, error, and cancellation cases. Verify value copying, shared references, actor isolation, and main-actor effects where relevant.
The Swift Programming Language — Swift.org