📖 Scenario: You are building a simple app that manages different types of vehicles. Each vehicle can describe itself with a message. You want to share common behavior among all vehicles without repeating code.
🎯 Goal: Create a protocol called Vehicle with a property and a method. Then use a protocol extension to provide a shared default implementation for the method. Finally, create two structs that conform to the protocol and use the shared behavior.
📋 What You'll Learn
Create a protocol named Vehicle with a var name: String { get } property and a func description() -> String method.
Add a protocol extension for Vehicle that provides a default implementation of description() returning a string using the name property.
Create a struct called Car that conforms to Vehicle and has a name property.
Create a struct called Bicycle that conforms to Vehicle and has a name property.
Create instances of Car and Bicycle and print their descriptions.
💡 Why This Matters
🌍 Real World
Protocol extensions let you add common behavior to many types in your app without repeating code. This is useful for apps managing different but related data types.
💼 Career
Understanding protocol extensions is important for Swift developers to write clean, reusable, and maintainable code in iOS and macOS apps.
Progress0 / 4 steps
1
Create the Vehicle protocol
Create a protocol called Vehicle with a read-only name property of type String and a method description() that returns a String.
Swift
Hint
Protocols define what properties and methods a type must have. Use protocol Vehicle { ... }.
2
Add a protocol extension with default description
Add a protocol extension for Vehicle that provides a default implementation of the description() method. It should return the string "This is a vehicle named \(name).".
Swift
Hint
Use extension Vehicle { func description() -> String { ... } } to add shared behavior.
3
Create Car and Bicycle structs conforming to Vehicle
Create a struct called Car with a name property of type String that conforms to Vehicle. Also create a struct called Bicycle with a name property of type String that conforms to Vehicle.
Swift
Hint
Use struct Car: Vehicle { let name: String } and similarly for Bicycle.
4
Create instances and print descriptions
Create a constant myCar as an instance of Car with the name "Tesla". Create a constant myBike as an instance of Bicycle with the name "Mountain Bike". Then print the result of calling description() on both myCar and myBike.
Swift
Hint
Create instances with let myCar = Car(name: "Tesla") and print with print(myCar.description()).
Practice
(1/5)
1. What is the main purpose of using protocol extensions in Swift?
easy
A. To store data inside protocols
B. To add shared behavior to multiple types without repeating code
C. To create new classes that inherit from protocols
D. To prevent types from conforming to protocols
Solution
Step 1: Understand protocol extensions
Protocol extensions allow adding default method or property implementations to protocols.
Step 2: Identify the benefit
This lets many types share behavior without rewriting the same code.
Final Answer:
To add shared behavior to multiple types without repeating code -> Option B
Quick Check:
Protocol extensions = shared behavior [OK]
Hint: Protocol extensions add default behavior to many types [OK]
Common Mistakes:
Thinking protocols can store data
Confusing protocol extensions with inheritance
Believing protocol extensions prevent conformance
2. Which of the following is the correct syntax to add a default method greet() to a protocol Greeter using an extension?
Person implements speak(), overriding the default. This is allowed and correct.
Final Answer:
No error; output is 'Hello' -> Option A
Quick Check:
Override default method by implementing in type [OK]
Hint: Implementing method in type overrides default extension [OK]
Common Mistakes:
Thinking default methods cause errors
Trying to use 'override' keyword in struct
Believing protocol methods must be optional
5. You want to add a shared method describe() to multiple unrelated types Cat and Dog without repeating code. Which approach using protocol extensions is best?
hard
A. Add a global function describe() that takes Any type
B. Add describe() method separately in Cat and Dog structs
C. Use class inheritance from a base class with describe() method
D. Create a protocol Animal with describe() and add default implementation in extension; make Cat and Dog conform
Solution
Step 1: Identify shared behavior need
Both Cat and Dog need the same describe() method without repeating code.
Step 2: Use protocol with extension
Define protocol Animal with describe(), then provide default describe() in protocol extension. Make Cat and Dog conform to Animal.
Final Answer:
Create a protocol Animal with describe() and add default implementation in extension; make Cat and Dog conform -> Option D
Quick Check:
Protocol extension = shared behavior for unrelated types [OK]
Hint: Use protocol + extension for shared methods across types [OK]
Common Mistakes:
Repeating code in each type
Using class inheritance when types are unrelated
Using global functions losing type safety
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.