0
0
Swiftprogramming~20 mins

Type erasure concept in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Type Erasure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of type-erased wrapper usage
What is the output of this Swift code using type erasure with AnySequence?
Swift
struct IntSequence: Sequence {
    func makeIterator() -> some IteratorProtocol {
        return (1...3).makeIterator()
    }
}

let seq: AnySequence<Int> = AnySequence(IntSequence())
for num in seq {
    print(num, terminator: " ")
}
A1 2 3
BSequence<Int>
CCompile-time error
D1 2 3 4
Attempts:
2 left
💡 Hint
Think about how AnySequence hides the underlying sequence type but still iterates correctly.
🧠 Conceptual
intermediate
1:30remaining
Purpose of type erasure in Swift
Why do Swift developers use type erasure?
ATo enable inheritance between unrelated classes
BTo hide concrete types and expose a common interface
CTo convert value types into reference types
DTo improve runtime performance by avoiding protocols
Attempts:
2 left
💡 Hint
Think about how protocols with associated types limit usage.
🔧 Debug
advanced
2:30remaining
Fix the error in this type erasure example
What error does this code produce and why? protocol Shape { associatedtype Color func draw() -> Color } struct AnyShape: Shape { private let _draw: () -> C init(_ shape: S) where S.Color == C { _draw = shape.draw } func draw() -> C { _draw() } } struct Circle: Shape { func draw() -> String { "Circle" } } let shape = AnyShape(Circle())
ANo error, code compiles successfully
BError: Cannot convert value of type 'Circle' to expected argument type 'Shape'
CError: Missing generic argument for 'AnyShape'
DError: 'Circle' does not conform to 'Shape' because 'Color' is not specified
Attempts:
2 left
💡 Hint
Check the associatedtype requirements in the protocol and the struct.
📝 Syntax
advanced
1:00remaining
Identify the syntax error in this type erasure wrapper
Which option correctly fixes the syntax error in this Swift type erasure code? struct AnyWrapper { private let _value: T init(_ value: T) { _value = value } func getValue() -> T { return _value } } let wrapper = AnyWrapper(5) print(wrapper.getValue()
AAdd a closing parenthesis after getValue(): print(wrapper.getValue())
BChange print to println: println(wrapper.getValue())
CAdd a semicolon after print: print(wrapper.getValue());
DReplace getValue() with getValue: print(wrapper.getValue)
Attempts:
2 left
💡 Hint
Check the parentheses balance in the print statement.
🚀 Application
expert
3:00remaining
How many elements does this type-erased sequence contain?
Consider this Swift code using type erasure. How many elements will be printed? struct EvenNumbers: Sequence { func makeIterator() -> some IteratorProtocol { (2...10).filter { $0 % 2 == 0 }.makeIterator() } } let numbers = AnySequence(EvenNumbers()) var count = 0 for _ in numbers { count += 1 } print(count)
A9
B0
C5
D10
Attempts:
2 left
💡 Hint
Count even numbers between 2 and 10 inclusive.