Challenge - 5 Problems
Swift Protocol Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol conformance with default implementation
What is the output of this Swift code when calling
dog.speak()?Swift
protocol Animal { func speak() -> String } extension Animal { func speak() -> String { return "Some sound" } } struct Dog: Animal { func speak() -> String { return "Woof!" } } let dog = Dog() print(dog.speak())
Attempts:
2 left
💡 Hint
Check if the struct provides its own implementation of the protocol method.
✗ Incorrect
The struct Dog provides its own implementation of speak(), so that version is called instead of the default from the protocol extension.
❓ Predict Output
intermediate2:00remaining
Protocol conformance with optional method
What will be printed when running this Swift code?
Swift
@objc protocol Greeter { @objc optional func greet() -> String } class Person: Greeter { } let p = Person() print(p.greet?() ?? "No greeting")
Attempts:
2 left
💡 Hint
Check if the class implements the optional method or not.
✗ Incorrect
Person does not implement greet(), so calling greet?() returns nil, triggering the nil-coalescing operator to print "No greeting".
🔧 Debug
advanced2:00remaining
Fix the protocol conformance error
This code does not compile. What is the cause of the error?
Swift
protocol Vehicle { var speed: Int { get set } func description() -> String } struct Car: Vehicle { var speed: Int func description() -> String { return "Car speed is \(speed)" } } let myCar = Car(speed: 100) print(myCar.description())
Attempts:
2 left
💡 Hint
Check the property requirements in the protocol and how Car declares speed.
✗ Incorrect
The protocol requires speed to be gettable and settable ({ get set }). Car declares speed as 'let', which only provides { get }, causing a conformance error.
❓ Predict Output
advanced2:00remaining
Protocol conformance with associated types
What is the output of this Swift code?
Swift
protocol Container { associatedtype Item var items: [Item] { get set } func count() -> Int } struct IntContainer: Container { var items: [Int] func count() -> Int { items.count } } let container = IntContainer(items: [1, 2, 3, 4]) print(container.count())
Attempts:
2 left
💡 Hint
Check how many items are in the array and what count() returns.
✗ Incorrect
IntContainer conforms to Container with Item as Int. The count() method returns the number of items in the array, which is 4.
❓ Predict Output
expert2:00remaining
Protocol conformance and method dispatch
What is the output of this Swift code?
Swift
protocol Speaker { func speak() -> String } struct Cat: Speaker { func speak() -> String { "Meow" } } func makeSpeak(_ speaker: Speaker) { print(speaker.speak()) } let cat = Cat() makeSpeak(cat)
Attempts:
2 left
💡 Hint
Check if Cat implements the required method and how protocol methods are called.
✗ Incorrect
Cat implements speak(), so when passed as Speaker, calling speak() prints "Meow".