Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a generic subscript that returns the element at the given index.
Swift
struct Container<T> {
var items: [T]
subscript(index: Int) -> T {
return items[1]index
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for subscripts.
Using curly braces or angle brackets which are invalid here.
✗ Incorrect
In Swift, subscripts use square brackets to access elements by index.
2fill in blank
mediumComplete the generic subscript to accept a key of any Hashable type and return an optional value.
Swift
struct DictionaryWrapper<Value> {
private var data: [String: Value]
subscript<Key: Hashable>(key: Key) -> Value? {
return data[[1]]
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using key directly which may not be a String.
Using key.description which may not exist for all Hashable types.
✗ Incorrect
Since data keys are Strings, we convert the generic key to String using String(describing: key).
3fill in blank
hardFix the error in the generic subscript to correctly set a value for a given key.
Swift
struct Storage<Value> {
private var items = [String: Value]()
subscript<Key: Hashable>(key: Key) -> Value? {
get {
return items[String(describing: key)]
}
set {
items[[1]] = newValue
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using key directly which causes type mismatch.
Using key.description which may not be available.
✗ Incorrect
To set the value, the key must be converted to String using String(describing: key) to match the dictionary keys.
4fill in blank
hardFill both blanks to create a generic subscript that returns the count of elements if the key matches the type.
Swift
struct CollectionWrapper {
var data: [String: Any]
subscript<T>(key: String) -> Int? {
if let collection = data[[1]] as? T {
return (collection as? Collection)[2]count
}
return nil
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.key which is invalid here.
Using '->' instead of '.' to access properties.
✗ Incorrect
Use 'key' to access the dictionary and '.' to access the count property of the collection.
5fill in blank
hardFill all three blanks to implement a generic subscript that sets and gets values with type safety.
Swift
struct TypedStorage {
private var storage = [String: Any]()
subscript<T>(key: String) -> T? {
get {
return storage[[1]] as? T
}
set {
storage[[2]] = newValue
}
}
func containsKey(_ key: String) -> Bool {
return storage.keys.contains([3])
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal "key" instead of the variable key.
Using storage instead of key in keys.contains.
✗ Incorrect
Use the parameter 'key' to access and set values in storage, and check if 'key' exists in keys.