Challenge - 5 Problems
Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Understanding Protocol Requirements
Given the protocol and struct below, what will be the output when calling
print(user.description)?iOS Swift
protocol Describable {
var description: String { get }
}
struct User: Describable {
var name: String
var description: String {
return "User: \(name)"
}
}
let user = User(name: "Alice")
print(user.description)Attempts:
2 left
💡 Hint
Check how the
description property is implemented in the struct.✗ Incorrect
The struct
User conforms to Describable by implementing the description property. It returns "User: Alice" when accessed.❓ ui_behavior
intermediate1:30remaining
Protocol and UIViewController Behavior
If a UIViewController conforms to a protocol with a method
setupUI() and calls it inside viewDidLoad(), what happens when the view loads?iOS Swift
protocol UISetup {
func setupUI()
}
class MyViewController: UIViewController, UISetup {
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
view.backgroundColor = .blue
}
}Attempts:
2 left
💡 Hint
Look at when
setupUI() is called and what it does.✗ Incorrect
The
setupUI() method is called inside viewDidLoad(), so the background color changes to blue when the view loads.❓ lifecycle
advanced2:00remaining
Protocol Extension and Method Dispatch
What will be printed when running the code below?
iOS Swift
protocol Greetable {
func greet()
}
extension Greetable {
func greet() {
print("Hello from protocol extension")
}
}
class Person: Greetable {
func greet() {
print("Hello from Person class")
}
}
let p: Greetable = Person()
p.greet()Attempts:
2 left
💡 Hint
Consider how Swift chooses methods when a class implements a protocol method and the protocol has a default implementation.
✗ Incorrect
Swift uses dynamic dispatch for class methods. Since
Person implements greet(), that method is called instead of the protocol extension's default.📝 Syntax
advanced2:00remaining
Protocol with Associated Type Syntax
Which option correctly declares a protocol with an associated type and a method using it?
Attempts:
2 left
💡 Hint
Look for the correct keyword to declare an associated type and method parameter syntax.
✗ Incorrect
Option A correctly uses
associatedtype and a method with a parameter labeled with underscore to omit external label, matching Swift syntax.🔧 Debug
expert2:30remaining
Resolving Protocol Conformance Ambiguity
Given the code below, what error will the compiler produce?
iOS Swift
protocol Drawable {
func draw()
}
extension Drawable {
func draw() {
print("Drawing from extension")
}
}
class Shape: Drawable {
}
let shape = Shape()
shape.draw()Attempts:
2 left
💡 Hint
Check if protocol extension provides a default implementation and if the class must implement it explicitly.
✗ Incorrect
The protocol extension provides a default implementation of
draw(), so Shape conforms without implementing it explicitly.