Challenge - 5 Problems
Protocol Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol extension method call
What is the output of this Swift code using protocol extensions?
Swift
protocol Greetable { func greet() -> String } extension Greetable { func greet() -> String { return "Hello from protocol extension" } } struct Person: Greetable { func greet() -> String { return "Hello from Person" } } struct Robot: Greetable {} let p = Person() let r = Robot() print(p.greet()) print(r.greet())
Attempts:
2 left
💡 Hint
Remember that structs can override protocol extension methods by implementing them directly.
✗ Incorrect
Person implements greet(), so its method is called. Robot does not implement greet(), so it uses the protocol extension's default implementation.
🧠 Conceptual
intermediate1:30remaining
Protocol extension default implementation usage
Which statement about protocol extensions providing default implementations is true?
Attempts:
2 left
💡 Hint
Think about how Swift chooses which method to call when both protocol extension and conforming type have the same method.
✗ Incorrect
If a conforming type implements a method, that implementation is used. Otherwise, the protocol extension's default is used.
🔧 Debug
advanced2:00remaining
Why does this protocol extension method not get called?
Given this code, why does the protocol extension method not get called when using a protocol-typed variable?
Swift
protocol Drawable { func draw() } extension Drawable { func draw() { print("Drawing from extension") } } struct Circle: Drawable { func draw() { print("Drawing Circle") } } let shape: Drawable = Circle() shape.draw()
Attempts:
2 left
💡 Hint
Consider how Swift dispatches protocol methods when called on a variable typed as the protocol.
✗ Incorrect
When a protocol method is called on a variable typed as the protocol, Swift uses dynamic dispatch and calls the conforming type's implementation if it exists.
📝 Syntax
advanced1:30remaining
Which option causes a syntax error in protocol extension?
Which of the following protocol extension code snippets will cause a syntax error?
Attempts:
2 left
💡 Hint
Check the return type and method used in the extension carefully.
✗ Incorrect
The reversed() method returns a ReversedCollection>, not an Array. Returning it as Array without conversion causes a type mismatch error.
🚀 Application
expert2:30remaining
How many items are in the resulting dictionary?
Consider this Swift code using protocol extensions and conforming types. How many key-value pairs does the dictionary contain after execution?
Swift
protocol Identifiable { var id: String { get } } extension Identifiable { func info() -> String { return "ID: \(id)" } } struct User: Identifiable { var id: String } struct Admin: Identifiable { var id: String func info() -> String { return "Admin ID: \(id)" } } let users: [Identifiable] = [User(id: "u1"), Admin(id: "a1"), User(id: "u2")] var dict = [String: String]() for user in users { dict[user.id] = user.info() } print(dict.count)
Attempts:
2 left
💡 Hint
Each user has a unique id used as dictionary key.
✗ Incorrect
The array has three Identifiable items with unique ids. The loop adds all three to the dictionary, so count is 3.