Subscripts in Swift allow access to type values by a key or index in a way similar to arrays or dictionaries, but for any of your own types. The syntax is similar to a function, but access is done in brackets: object[key].
Features:
get, or get and set).Application:
Example: A two-dimensional array (matrix) and a subscript with two parameters:
struct Matrix { let rows: Int, columns: Int private var grid: [Double] init(rows: Int, columns: Int) { self.rows = rows self.columns = columns grid = Array(repeating: 0.0, count: rows * columns) } subscript(row: Int, column: Int) -> Double { get { precondition(isValid(row: row, column: column), "Index out of range") return grid[(row * columns) + column] } set { precondition(isValid(row: row, column: column), "Index out of range") grid[(row * columns) + column] = newValue } } private func isValid(row: Int, column: Int) -> Bool { return row >= 0 && row < rows && column >= 0 && column < columns } } var matrix = Matrix(rows: 2, columns: 2) matrix[0,1] = 5.0 print(matrix[0,1]) // 5.0
Can a subscript have an inout parameter or be mutating in a struct?
Answer:
inout.mutating set, allowing modifications of self inside set:struct Counter { var value: Int = 0 subscript(increment: Bool) -> Int { mutating get { value += increment ? 1 : -1 return value } } }
Story
In a custom collection, they forgot to make the set in the mutating subscript in the struct, resulting in any attempt to modify an element via subscript causing a compilation error. The error manifested during the integration of the collection after long debugging.
Story
In a custom subscript, they did not implement proper handling for out-of-bounds access, which caused crashes when accessing out of range. As a result, business-critical data processing functions failed.
Story
They implemented a subscript with two parameters in an enum for caching, but did not consider that a subscript cannot be static (on the type) but only on the instance. The architectural decision turned out to be erroneous, and the API interaction of the collection had to be rewritten.