0
0
Swiftprogramming~20 mins

Protocol with associated types 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 a protocol with associated type conformance
What is the output of this Swift code using a protocol with an associated type?
Swift
protocol Container {
    associatedtype Item
    mutating func append(_ item: Item)
    func count() -> Int
    subscript(i: Int) -> Item { get }
}

struct IntStack: Container {
    var items = [Int]()
    mutating func append(_ item: Int) {
        items.append(item)
    }
    func count() -> Int {
        items.count
    }
    subscript(i: Int) -> Int {
        items[i]
    }
}

var stack = IntStack()
stack.append(10)
stack.append(20)
print(stack.count())
print(stack[1])
A2\n20
B1\n10
C2\n10
D1\n20
Attempts:
2 left
💡 Hint
Think about how many items are appended and what is at index 1.
🧠 Conceptual
intermediate
1:30remaining
Understanding associatedtype constraints
Given the protocol below, which statement about the associatedtype is true?
Swift
protocol Identifiable {
    associatedtype ID: Comparable
    var id: ID { get }
}
AThe associatedtype ID must be an Int.
BThe associatedtype ID must conform to Comparable, so any type used must support comparison.
CThe associatedtype ID must be a class type.
DThe associatedtype ID can be any type without restrictions.
Attempts:
2 left
💡 Hint
Look at the ': Comparable' part in the associatedtype declaration.
🔧 Debug
advanced
2:00remaining
Identify the error in protocol with associated type usage
What error will this Swift code produce?
Swift
protocol Container {
    associatedtype Item
    mutating func append(_ item: Item)
}

struct StringStack: Container {
    var items = [String]()
    mutating func append(_ item: Int) {
        items.append(String(item))
    }
}
AMissing mutating keyword on append method
BNo error, code compiles successfully
CCannot convert value of type 'Int' to expected argument type 'String'
DType 'StringStack' does not conform to protocol 'Container' because 'append' parameter type does not match associatedtype 'Item'
Attempts:
2 left
💡 Hint
Check the parameter type of append compared to the associatedtype Item.
📝 Syntax
advanced
2:00remaining
Correct syntax for protocol with associated type and default implementation
Which option correctly defines a protocol with an associated type and provides a default implementation for a method?
A
protocol Summable {
    associatedtype Number
    func sum(_ a: Number, _ b: Number) -> Number
}
extension Summable {
    func sum(_ a: Number, _ b: Number) -> Number {
        return a - b
    }
}
B
protocol Summable {
    associatedtype Number
    func sum(_ a: Number, _ b: Number) -> Number
}
extension Summable {
    func sum(a: Number, b: Number) -> Number {
        return a + b
    }
}
C
protocol Summable {
    associatedtype Number
    func sum(_ a: Number, _ b: Number) -> Number
}
extension Summable {
    func sum(_ a: Number, _ b: Number) -> Number {
        return a + b
    }
}
D
protocol Summable {
    associatedtype Number
    func sum(_ a: Number, _ b: Number) -> Number
}
extension Summable {
    func sum(_ a: Number, _ b: Number) -> Number {
        return a * b
    }
}
Attempts:
2 left
💡 Hint
Check method signature and operator used in default implementation.
🚀 Application
expert
2:30remaining
Implement a generic function using protocol with associated types
Which option correctly implements a generic function that takes any Container and returns the first item?
Swift
protocol Container {
    associatedtype Item
    func count() -> Int
    subscript(i: Int) -> Item { get }
}

func firstItem<C: Container>(_ container: C) -> C.Item? {
    // Fill in the blank
}
Areturn container.count() > 0 ? container[0] : nil
Breturn container.count > 0 ? container[0] : nil
Creturn container[0]
Dreturn container.first
Attempts:
2 left
💡 Hint
Check how to safely access the first element using count and subscript.