Challenge - 5 Problems
Swift Protocol Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol inheritance with default implementation
What is the output of this Swift code using protocol inheritance and default implementations?
Swift
protocol A { func greet() -> String } protocol B: A { func greet() -> String } extension A { func greet() -> String { return "Hello from A" } } extension B { func greet() -> String { return "Hello from B" } } struct MyStruct: B {} let obj = MyStruct() print(obj.greet())
Attempts:
2 left
💡 Hint
Remember that protocol inheritance allows overriding default implementations in child protocols.
✗ Incorrect
The struct MyStruct conforms to protocol B, which inherits from A. Both A and B provide default implementations of greet(). The implementation in B overrides the one in A, so calling greet() on MyStruct uses B's version.
❓ Predict Output
intermediate2:00remaining
Value of property from multiple inherited protocols
What is the value of the property 'description' when accessed from an instance of struct C?
Swift
protocol P1 { var description: String { get } } protocol P2: P1 { var description: String { get } } extension P1 { var description: String { "P1" } } extension P2 { var description: String { "P2" } } struct C: P2 {} let c = C() print(c.description)
Attempts:
2 left
💡 Hint
Check which protocol's extension provides the property implementation that is used.
✗ Incorrect
Since P2 inherits from P1 and provides its own default implementation of description, the struct C uses P2's version.
🔧 Debug
advanced2:00remaining
Identify the error in protocol inheritance with conflicting requirements
What error does this Swift code produce?
Swift
protocol Alpha { func doSomething() -> Int } protocol Beta { func doSomething() -> String } protocol Gamma: Alpha, Beta {} struct Test: Gamma { func doSomething() -> Int { return 42 } }
Attempts:
2 left
💡 Hint
Check the return types required by each protocol and what the struct implements.
✗ Incorrect
Protocol Beta requires doSomething() returning String, but Test only implements the version returning Int, so it does not conform to Beta.
📝 Syntax
advanced1:00remaining
Correct syntax for protocol inheritance declaration
Which option shows the correct syntax to declare a protocol 'C' that inherits from protocols 'A' and 'B'?
Attempts:
2 left
💡 Hint
Swift uses a colon ':' to declare protocol inheritance.
✗ Incorrect
In Swift, protocol inheritance is declared using a colon followed by a comma-separated list of protocols.
🚀 Application
expert3:00remaining
Determine the number of methods required by a conforming type
Given these protocols and extensions, how many methods must a struct implement to conform to protocol Z?
Swift
protocol X { func method1() } protocol Y: X { func method2() } protocol Z: Y { func method3() } extension X { func method1() { print("X method1") } } extension Y { func method2() { print("Y method2") } } // No extension for Z struct MyStruct: Z { func method3() { print("Z method3") } }
Attempts:
2 left
💡 Hint
Check which methods have default implementations and which do not.
✗ Incorrect
method1 and method2 have default implementations in protocol extensions X and Y. Only method3 lacks a default and must be implemented.