0
0
Swiftprogramming~20 mins

Type constraints with protocol conformance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Protocol Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of generic function with protocol constraint

What is the output of this Swift code?

Swift
protocol Describable {
    func describe() -> String
}

struct Person: Describable {
    var name: String
    func describe() -> String {
        return "Person named \(name)"
    }
}

func printDescription<T: Describable>(_ item: T) {
    print(item.describe())
}

let p = Person(name: "Alice")
printDescription(p)
ANo output
BPerson named p
CError: Type 'Person' does not conform to protocol 'Describable'
DPerson named Alice
Attempts:
2 left
💡 Hint

Check how the describe() method is implemented and called.

🧠 Conceptual
intermediate
1:30remaining
Understanding protocol conformance in type constraints

Which statement about Swift type constraints with protocol conformance is true?

AA generic type constrained to a protocol can only accept types that implement all protocol requirements.
BProtocols cannot be used as constraints in generic functions.
CA generic type constrained to a protocol can accept any type, regardless of conformance.
DType constraints with protocols allow only classes, not structs or enums.
Attempts:
2 left
💡 Hint

Think about what it means to constrain a generic type to a protocol.

🔧 Debug
advanced
2:00remaining
Identify the error in protocol conformance with type constraints

What error does this Swift code produce?

Swift
protocol Runnable {
    func run()
}

struct Car: Runnable {
    func run() {
        print("Car is running")
    }
}

func start<T: Runnable>(_ vehicle: T) {
    vehicle.run()
}

struct Bike {}

start(Bike())
ARuntime error: method 'run()' not found
BError: Missing implementation of 'run()' in 'Bike'
CError: Type 'Bike' does not conform to protocol 'Runnable'
DNo error, prints 'Car is running'
Attempts:
2 left
💡 Hint

Check the type passed to the generic function and its protocol conformance.

📝 Syntax
advanced
1:30remaining
Correct syntax for protocol conformance in generic constraints

Which option shows the correct syntax to constrain a generic type T to conform to protocol Equatable?

Afunc compare<T: Equatable>(a: T, b: T) -> Bool { return a == b }
Bfunc compare<T where T: Equatable>(a: T, b: T) -> Bool { return a == b }
Cfunc compare<T: Equatable where T == Equatable>(a: T, b: T) -> Bool { return a == b }
Dfunc compare<T: Equatable>(a: T, b: T) -> Bool { return a.equals(b) }
Attempts:
2 left
💡 Hint

Recall the syntax for generic constraints in Swift function declarations.

🚀 Application
expert
2:30remaining
Determine the number of items in the filtered array

Given the following Swift code, how many items are in the filtered array after execution?

Swift
protocol Filterable {
    var isActive: Bool { get }
}

struct Item: Filterable {
    var isActive: Bool
}

func filterActive<T: Filterable>(_ items: [T]) -> [T] {
    items.filter { $0.isActive }
}

let items = [Item(isActive: true), Item(isActive: false), Item(isActive: true), Item(isActive: false)]
let filtered = filterActive(items)
print(filtered.count)
A4
B2
C0
D3
Attempts:
2 left
💡 Hint

Count how many Item instances have isActive set to true.