What is protocol conformance via extension in Swift?
It means adding the ability for a type to follow a protocol by writing the required methods or properties inside an extension, not in the original type definition.
Click to reveal answer
beginner
Why use extensions to conform to protocols in Swift?
Extensions help organize code by separating protocol requirements from the main type code, making it cleaner and easier to read.
Click to reveal answer
intermediate
Can you add stored properties in an extension to conform to a protocol?
No, extensions cannot add stored properties. They can only add computed properties, methods, or make a type conform to a protocol.
Click to reveal answer
beginner
Example: How to make a struct conform to a protocol using an extension?
Define the struct first, then create an extension for it that implements the protocol's required methods or properties.
Click to reveal answer
intermediate
What happens if a type already conforms to a protocol but you add conformance again in an extension?
Swift will give an error because a type can only conform to a protocol once. You can add methods in extensions but not duplicate conformance.
Click to reveal answer
What can you NOT add in a Swift extension to help conform to a protocol?
AStored properties
BComputed properties
CMethods
DProtocol conformance
✗ Incorrect
Extensions cannot add stored properties, only computed properties, methods, or protocol conformance.
Why might you use an extension to conform to a protocol?
ATo delete existing methods
BTo add stored properties
CTo change the original type's stored properties
DTo organize code better
✗ Incorrect
Extensions help organize code by separating protocol conformance from the main type definition.
If a struct conforms to a protocol in an extension, where must the protocol methods be implemented?
AInside the extension
BInside the original struct definition
COutside both the struct and extension
DIn a different file only
✗ Incorrect
When conforming via extension, the required protocol methods must be implemented inside that extension.
Can a class conform to multiple protocols using multiple extensions?
AOnly if the class is final
BNo, only one protocol per class is allowed
CYes, each extension can add conformance to a different protocol
DOnly if the protocols are related
✗ Incorrect
A class can conform to multiple protocols by using separate extensions for each protocol.
What error occurs if you try to declare the same protocol conformance twice for a type?
ASyntax error
BDuplicate conformance error
CRuntime crash
DNo error, it is allowed
✗ Incorrect
Swift does not allow duplicate protocol conformance declarations for the same type.
Explain how protocol conformance via extension works in Swift and why it is useful.
Think about how extensions let you add features without changing the original type.
You got /3 concepts.
Describe the limitations of extensions when adding protocol conformance in Swift.
Remember what extensions can and cannot do compared to the original type.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using an extension to conform a type to a protocol in Swift?
easy
A. To add protocol requirements to a type without modifying its original definition
B. To create a new subclass from an existing class
C. To override existing methods in a class
D. To declare a new protocol
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 A
Quick Check:
Extensions add protocol conformance without altering original type [OK]
Hint: Extensions add protocol methods without touching original code [OK]
Common Mistakes:
Thinking extensions create subclasses
Confusing extensions with method overriding
Believing extensions declare new protocols
2. Which of the following is the correct syntax to make a struct Car conform to protocol Drivable using an extension?
easy
A. protocol Drivable extension Car { func drive() { print("Driving") } }
4. The following code tries to make 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
}
}
medium
A. Method 'area()' should be a computed property, not a function
B. Cannot extend a struct to conform to a protocol
C. Missing initializer in Rectangle
D. Protocol Shape requires a method named 'area()', not a property
Solution
Step 1: Check protocol requirement
Shape requires a property area of type Double with a getter.
Step 2: Analyze extension implementation
The extension implements area() as a method, but protocol expects a computed property var area: Double { get }.
Final Answer:
Method 'area()' should be a computed property, not a function -> Option A
Quick Check:
Property requirement needs computed property, not method [OK]
Hint: Match protocol property with computed property, not method [OK]
Common Mistakes:
Implementing property as method
Thinking structs can't conform via extension
Ignoring protocol property vs method difference
5. You have a protocol 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
}
}
hard
A. class Book: Printable {
func printDetails() {
print(title + author)
}
}
D. extension Printable: Book {
func printDetails() {
print(title + 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.