Challenge - 5 Problems
Generic Subscript Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic subscript accessing dictionary
What is the output of this Swift code using a generic subscript to access dictionary values?
Swift
struct Container { private var data = ["a": 1, "b": 2, "c": 3] subscript<T>(key: T) -> Int? where T == String { return data[key] } } let container = Container() print(container["b"] ?? 0)
Attempts:
2 left
💡 Hint
The subscript returns an Int? but the print unwraps it with ?? operator.
✗ Incorrect
The subscript returns an optional Int. Using ?? 0 unwraps it or returns 0. Since key "b" exists with value 2, it prints 2.
❓ Predict Output
intermediate2:00remaining
Generic subscript with multiple constraints output
What will this Swift code print when using a generic subscript constrained to Numeric & Comparable types?
Swift
struct Numbers { private var values = [10, 20, 30, 40] subscript<T: Numeric & Comparable>(index: T) -> T? { guard let i = index as? Int, i < values.count else { return nil } return values[i] as? T } } let nums = Numbers() print(nums[2] ?? -1)
Attempts:
2 left
💡 Hint
The subscript converts generic index to Int and returns the value if valid.
✗ Incorrect
Index 2 is valid, so values[2] = 30 is returned and printed.
🔧 Debug
advanced2:00remaining
Identify the error in generic subscript declaration
This Swift struct uses a generic subscript but causes a compile error. What is the error?
Swift
struct Box { var items = ["apple", "banana"] subscript<T>(index: T) -> String { return items[index] } }
Attempts:
2 left
💡 Hint
Array subscripts require Int index, generic T is unconstrained.
✗ Incorrect
The subscript uses generic T as index but array requires Int index. Without constraint or cast, this causes a compile error.
📝 Syntax
advanced2:00remaining
Which generic subscript syntax is correct?
Which option shows the correct syntax for a generic subscript in Swift that returns the count of elements for a generic collection?
Attempts:
2 left
💡 Hint
Generic constraints use 'where' clause after return type.
✗ Incorrect
Option B correctly uses 'where T: Collection' after the return type. Option B places constraint incorrectly. Options C and D have syntax errors.
🚀 Application
expert3:00remaining
Using generic subscripts to access heterogeneous dictionary values
Given this Swift struct with a generic subscript, what will be the output of the print statements?
Swift
struct AnyDict { private var dict: [String: Any] = ["int": 42, "str": "hello", "dbl": 3.14] subscript<T>(key: String) -> T? { return dict[key] as? T } } let anyDict = AnyDict() print(anyDict["int"] as Int?) print(anyDict["str"] as String?) print(anyDict["dbl"] as Double?) print(anyDict["missing"] as Int?)
Attempts:
2 left
💡 Hint
The subscript casts Any to requested type and returns optional.
✗ Incorrect
Each print prints the optional returned by the generic subscript. Existing keys return Optional values, missing key returns nil.