Challenge - 5 Problems
Swift Associated Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of protocol with associated type and conforming struct
What is the output of this Swift code when run?
Swift
protocol Container { associatedtype Item var items: [Item] { get } func firstItem() -> Item? } struct IntContainer: Container { var items: [Int] func firstItem() -> Int? { return items.first } } let container = IntContainer(items: [10, 20, 30]) print(container.firstItem() ?? 0)
Attempts:
2 left
💡 Hint
Look at what the firstItem() function returns from the items array.
✗ Incorrect
The IntContainer struct conforms to Container with Item as Int. The firstItem() returns the first element of items, which is 10.
❓ Predict Output
intermediate2:00remaining
Output of generic function using protocol with associated type
What will be printed by this Swift code?
Swift
protocol Summable { associatedtype Number var values: [Number] { get } func sum() -> Number } struct IntSum: Summable { var values: [Int] func sum() -> Int { values.reduce(0, +) } } func printSum<T: Summable>(_ container: T) { print(container.sum()) } let intSum = IntSum(values: [1, 2, 3, 4]) printSum(intSum)
Attempts:
2 left
💡 Hint
The sum() function adds all elements in values.
✗ Incorrect
The sum() method adds 1+2+3+4 = 10, so printSum prints 10.
🔧 Debug
advanced2:00remaining
Identify the error in protocol conformance with associated type
Why does this Swift code fail to compile?
Swift
protocol Identifiable { associatedtype ID var id: ID { get } } struct User: Identifiable { var id: String var name: String } struct Product: Identifiable { var id: Int var price: Double } func printID<T: Identifiable>(_ item: T) { print(item.id) } let user = User(id: "abc123", name: "Alice") let product = Product(id: 101, price: 9.99) printID(user) printID(product)
Attempts:
2 left
💡 Hint
Check if the protocol requirements are met by the structs.
✗ Incorrect
Both User and Product conform to Identifiable by providing an id property of their own type. The generic function printID works for both types.
📝 Syntax
advanced2:00remaining
Which option correctly declares a protocol with an associated type and a method using it?
Select the correct Swift syntax for a protocol with an associated type named Element and a method that returns an Element.
Attempts:
2 left
💡 Hint
associatedtype declares a placeholder type in protocols.
✗ Incorrect
Option C correctly uses associatedtype and a method returning Element. Option C uses typealias which is invalid here. Options C and D change the return type to optional, which is not what the prompt asks.
🚀 Application
expert2:00remaining
How many items are in the resulting array after filtering with associated type constraint?
Given this Swift code, how many elements does the filtered array contain after applying the filter?
Swift
protocol Filterable { associatedtype Item var items: [Item] { get } func filterItems(_ predicate: (Item) -> Bool) -> [Item] } struct StringFilter: Filterable { var items: [String] func filterItems(_ predicate: (String) -> Bool) -> [String] { items.filter(predicate) } } let filter = StringFilter(items: ["apple", "banana", "cherry", "date"]) let result = filter.filterItems { $0.count > 5 } print(result.count)
Attempts:
2 left
💡 Hint
Count the strings with more than 5 characters.
✗ Incorrect
Only "banana" and "cherry" have more than 5 characters, so the filtered array has 2 elements.