Protocol extensions let you add common behavior to many types at once. Default implementations mean you don't have to write the same code again and again.
When you want many types to share the same behavior without repeating code.
When you want to add new features to a protocol without breaking existing code.
When you want to provide a simple default that most types can use but still allow customization.
When you want to keep your code clean and easy to maintain by avoiding duplication.
Hint: Use 'extension ProtocolName' to add default methods [OK]
Common Mistakes:
Trying to put method bodies inside protocol declaration
Using 'extension' inside protocol block
Omitting the 'extension' keyword
3. Consider the code below:
protocol Speaker {
func speak()
}
extension Speaker {
func speak() {
print("Speaking from extension")
}
}
struct Person: Speaker {
func speak() {
print("Person speaking")
}
}
struct Robot: Speaker {}
let p = Person()
p.speak()
let r = Robot()
r.speak()
What is the output when this code runs?
medium
A. Person speaking
Robot speaking
B. Speaking from extension
Speaking from extension
C. Person speaking
Speaking from extension
D. Compilation error
Solution
Step 1: Understand method resolution for protocol extensions
If a conforming type provides its own method, it overrides the default from the extension.
Step 2: Analyze the structs
Person implements speak() so it prints "Person speaking". Robot does not implement it, so uses default "Speaking from extension".
Final Answer:
Person speaking
Speaking from extension -> Option C
Quick Check:
Own method overrides default extension [OK]
Hint: Own method overrides default extension method [OK]
Common Mistakes:
Assuming default is always called
Expecting Robot to cause error
Confusing protocol declaration with implementation
4. The following code tries to add a default implementation for describe() in a protocol extension, but it causes a compile error. What is the mistake?
B. Missing parentheses after method name in extension
C. Extension cannot provide method implementations
D. Missing 'func' keyword in extension
Solution
Step 1: Check method syntax in extension
Method definitions require parentheses even if no parameters are present.
Step 2: Identify the error
The extension method func describe is missing parentheses () after the name.
Final Answer:
Missing parentheses after method name in extension -> Option B
Quick Check:
Method syntax needs () even if empty [OK]
Hint: Always include () after method name in definitions [OK]
Common Mistakes:
Omitting parentheses in method declaration
Thinking protocol methods must have bodies
Believing extensions can't implement methods
5. You want to create a protocol Identifiable with a property id and a default method identify() that prints the id. How would you write the protocol and extension so that conforming types can use the default identify() without extra code?
hard
A. protocol Identifiable { var id: String { get } }\nextension Identifiable { func identify() { print(\"ID: \(id)\") } }
B. protocol Identifiable { func identify() }\nextension Identifiable { var id: String { return \"default\" } }
C. protocol Identifiable { var id: String }\nextension Identifiable { func identify() { print(id) } }
D. protocol Identifiable { var id: String { get set } }\nextension Identifiable { func identify() { print(\"ID: \(id)\") } }"
Solution
Step 1: Define protocol with read-only property
Use var id: String { get } to require an id property that conforming types must provide.
Step 2: Provide default identify() method in extension
Extension adds identify() that prints the id using string interpolation.
Final Answer:
protocol Identifiable { var id: String { get } }\nextension Identifiable { func identify() { print("ID: \(id)") } } -> Option A