Activity 1
Define a capability
Create a Describable protocol with a description property, then conform two unrelated structs to it.
Hint: Focus on what callers need rather than all data each type stores.
Swift Fundamentals
Define shared capabilities and add focused behavior without changing original declarations.
Before you begin
Learning stage
A protocol describes requirements that a conforming type must satisfy, such as properties or methods. It expresses capability without specifying one concrete implementation type.
An extension adds methods, computed properties, initializers, or conformances to an existing type. A protocol extension can provide shared implementations to all conforming types.
Learning stage
Two different models conform to the same progress-reporting capability and receive a shared summary.
protocol ProgressReporting {
var completedUnits: Int { get }
var totalUnits: Int { get }
}
extension ProgressReporting {
var progressText: String {
"\(completedUnits) of \(totalUnits) complete"
}
}
struct Course: ProgressReporting {
let completedUnits: Int
let totalUnits: Int
}
let swift = Course(completedUnits: 4, totalUnits: 11)
print(swift.progressText)Learning stage
These concepts appear throughout production iOS code. Focus on the requirement each technique communicates.
Learning stage
Notice these mistakes early so the compiler and type system can help instead of becoming obstacles.
Learning stage
Work through these activities in a small Xcode project. No embedded playground is required on this site.
Activity 1
Create a Describable protocol with a description property, then conform two unrelated structs to it.
Hint: Focus on what callers need rather than all data each type stores.
Activity 2
Add a protocol extension method that prints or returns a formatted description for every conforming value.
Hint: Build the default from protocol requirements.
Learning stage
Answer each question before revealing the explanation.
QUESTION 1
No. It defines requirements or capabilities that conforming types implement.
QUESTION 2
No
Extensions can add computed properties and behavior, but not new stored properties.
Learning stage
Explain the idea in terms of its purpose, behavior, and tradeoffs rather than reciting syntax.
Short answer: Protocols let code depend on required capabilities instead of one concrete type.
A protocol establishes a contract that multiple types can satisfy. This supports substitution, focused interfaces, testing seams, and generic algorithms. Protocol extensions can share behavior while conforming types retain their own data and identity.
Possible follow-ups
Learning stage
Protocols create capability boundaries; extensions add focused behavior and conformance. Introduce them when they simplify real variation, not only to add abstraction.