0
0
Swiftprogramming~10 mins

Generic subscripts in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A{index}
B(index)
C[index]
D<index>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for subscripts.
Using curly braces or angle brackets which are invalid here.
2fill in blank
medium

Complete 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'
Akey.description
Bkey
CString(key)
DString(describing: key)
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.
3fill in blank
hard

Fix 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'
AString(describing: key)
BString(key)
Ckey
Dkey.description
Attempts:
3 left
💡 Hint
Common Mistakes
Using key directly which causes type mismatch.
Using key.description which may not be available.
4fill in blank
hard

Fill 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'
Akey
Bself.key
C.
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using self.key which is invalid here.
Using '->' instead of '.' to access properties.
5fill in blank
hard

Fill 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'
Akey
B"key"
Dstorage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal "key" instead of the variable key.
Using storage instead of key in keys.contains.