Protocol conformance via extension lets you add behavior to a type without changing its original code. It helps keep your code organized and flexible.
Protocol conformance via extension in Swift
extension TypeName: ProtocolName { // implement protocol requirements here }
You write extension followed by the type name and the protocol name.
Inside the braces, you add the methods or properties the protocol needs.
Person conform to Greetable by adding greet() in an extension.protocol Greetable { func greet() } struct Person { let name: String } extension Person: Greetable { func greet() { print("Hello, \(name)!") } }
Describable conformance to Int by providing a computed property description.protocol Describable { var description: String { get } } extension Int: Describable { var description: String { return "Number \(self)" } }
This program defines a Printable protocol and makes Book conform to it using an extension. Then it prints the book details.
protocol Printable { func printDetails() } struct Book { let title: String let author: String } extension Book: Printable { func printDetails() { print("Book: \(title) by \(author)") } } let myBook = Book(title: "Swift Basics", author: "Jane Doe") myBook.printDetails()
Extensions can add protocol conformance even to types you do not own, like standard library types.
If the protocol has required initializers, you cannot add them in extensions.
Using extensions helps keep your main type code clean and focused.
Use extensions to make types conform to protocols without changing original code.
This keeps code organized and allows adding features later.
Extensions can add methods and properties required by protocols.
Practice
extension to conform a type to a protocol in Swift?Solution
Step 1: Understand protocol conformance
Protocols define a set of methods or properties a type must implement.Step 2: Role of extensions
Extensions allow adding these implementations to a type without changing its original code.Final Answer:
To add protocol requirements to a type without modifying its original definition -> Option AQuick Check:
Extensions add protocol conformance without altering original type [OK]
- Thinking extensions create subclasses
- Confusing extensions with method overriding
- Believing extensions declare new protocols
Car conform to protocol Drivable using an extension?Solution
Step 1: Identify correct extension syntax
Extensions use the format:extension TypeName: ProtocolName { ... }.Step 2: Check each option
extension Car: Drivable { func drive() { print("Driving") } } correctly extendsCarto conform toDrivableand implementsdrive().Final Answer:
extension Car: Drivable { func drive() { print("Driving") } } -> Option DQuick Check:
Correct extension syntax = extension Car: Drivable { func drive() { print("Driving") } } [OK]
- Placing extension inside struct
- Swapping type and protocol names
- Using 'protocol' keyword incorrectly
protocol Greetable {
func greet() -> String
}
struct Person {
let name: String
}
extension Person: Greetable {
func greet() -> String {
return "Hello, \(name)!"
}
}
let p = Person(name: "Anna")
print(p.greet())What is the output when this code runs?
Solution
Step 1: Check protocol conformance
Person is extended to conform to Greetable by implementing greet() returning "Hello, \(name)!".Step 2: Analyze output
Creating Person with name "Anna" and calling greet() returns "Hello, Anna!" which is printed.Final Answer:
"Hello, Anna!" -> Option CQuick Check:
Extension adds greet() returning personalized string [OK]
- Expecting error due to missing conformance
- Ignoring string interpolation
- Assuming default greet() implementation
Rectangle conform to Shape via extension, but it causes a compile error. What is the error?protocol Shape {
var area: Double { get }
}
struct Rectangle {
var width: Double
var height: Double
}
extension Rectangle: Shape {
func area() -> Double {
return width * height
}
}Solution
Step 1: Check protocol requirement
Shape requires a propertyareaof typeDoublewith a getter.Step 2: Analyze extension implementation
The extension implementsarea()as a method, but protocol expects a computed propertyvar area: Double { get }.Final Answer:
Method 'area()' should be a computed property, not a function -> Option AQuick Check:
Property requirement needs computed property, not method [OK]
- Implementing property as method
- Thinking structs can't conform via extension
- Ignoring protocol property vs method difference
Printable requiring a method printDetails(). You want to add this conformance to an existing class Book without changing its original code. Which approach correctly achieves this and allows printDetails() to print the book's title and author?class Book {
let title: String
let author: String
init(title: String, author: String) {
self.title = title
self.author = author
}
}Solution
Step 1: Understand requirement
We want to add Printable conformance to Book without changing Book's original code.Step 2: Use extension to conform
extension Book: Printable { func printDetails() { print("Title: \(title), Author: \(author)") } } extends Book to conform to Printable and implements printDetails() accessing Book's properties.Final Answer:
extension Book: Printable { func printDetails() { print("Title: \(title), Author: \(author)") } } -> Option BQuick Check:
Extension adds protocol conformance and method using existing properties [OK]
- Trying to modify original class code
- Incorrectly extending protocol instead of class
- Not implementing required method
Ready to prove it?
Pass the test and this pattern moves unknown
