Swift Subscripts
Subscripts, Type Subscripts
1. Subscripts π©βπ»
Classes, Structures, Enumerations λ μ΄κ²λ€μ΄ κ°μ§κ³ μλ Collection, List, Sequence μ κ°μ
member elements
μ μ κ·ΌνκΈ° μν λ¨μΆν€μΈ Subscripts
λ₯Ό μ μν μ μλ€.
νλμ Type μ μ¬λ¬ κ°μ Subscripts λ₯Ό μ μν μ μμΌλ©°, Subscripts μ μ λ¬νλ index μ Type μ λ°λΌ
overload
μ²λ¦¬ νλ€. λν Subscripts λ λ¨μΌ μ°¨μμΌλ‘ μ νλμ§ μκ³ Custom Type
μ λ§μΆ° μ¬λ¬ κ°μ νλΌλ―Έν°λ‘
Subscripts
λ₯Ό μ μν μ μλ€.
1. Subscript Syntax
Syntax
subscript(index: Int) -> Int {
get {
// Return an appropriate subscript value here.
}
set(newValue) {
// Perform a suitable setting action here.
}
}
Computed Properties μ λ§μ°¬κ°μ§λ‘
getter
μoptional setter
λ₯Ό μ 곡νλ©°, setter μ Parameter λ₯Ό μλ΅νκ³ κΈ°λ³Έκ°μΌλ‘newValue
λ₯Ό μ¬μ©ν μ μλ€.
λν Computed Properties μ λ§μ°¬κ°μ§λ‘ setter μ Parameter λ λ°λμ Return Type κ³Ό λμΌν΄μΌνλ―λ‘ λ³λμType
μ λͺ μν μ μμΌλ©°, Read-Only Computed Propertiesμ λ§μ°¬κ°μ§λ‘Read-Only Subscripts
λget
ν€μλμ μ€κ΄νΈλ₯Ό μλ΅ν μ μλ€.
λ€μμ μ μμ n-times-table
μ νμνκΈ° μν΄ TimesTable Structure
λ₯Ό μ μνλ€. Subscripts λ
Read-Only Subsscripts
λ‘ κ΅¬νλμλ€.
struct TimesTable {
let multiplier: Int
subscript(index: Int) -> Int {
multiplier * index
}
}
let threeTimesTable = TimesTable(multiplier: 3)
(0...10).forEach { print(threeTimesTable[$0], terminator: " ") }
0 3 6 9 12 15 18 21 24 27 30
2. Subscript Usage
Subscripts λ ꡬννλ €λ Classes, Structures, Enumerations μ μ ν©ν ννλ‘ μμ λ‘κ² κ΅¬νμ΄ κ°λ₯νλ€.
λ°λΌμ, Subscripts μ μ νν μλ―Έλ context
μ λ°λΌ λ¬λΌμ§λ€. μΌλ°μ μΌλ‘ Subscripts λ Collection,
List, Sequenceμ member elements
μ μ κ·ΌνκΈ° μν μ©λλ‘ μ¬μ©λλ€.
- Subscripts in Dictionary
Subscripts λ₯Ό μ΄μ©ν΄ κ°μ μ‘°ννκΈ°
var numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
print("The number of legs of ant is \(numberOfLegs["ant"]!).")
// The number of legs of ant is 6.
Subscripts λ₯Ό μ΄μ©ν΄ κ°μ μ μ₯νκΈ°
numberOfLegs["bird"] = 2
print(numberOfLegs) // ["spider": 8, "ant": 6, "cat": 4, "bird": 2]
Dictionary
μkey-value
λ λͺ¨λ keys κ° values λ₯Ό κ°μ§ μλ κ²μ λͺ¨λΈλ‘ νκΈ° λλ¬ΈμOptional Return Type
μ μ·¨νλ―λ‘Optional Subscripts
λ₯Ό μ¬μ©νλ€.
3. Subscript Options
Subscripts λ Parameters μ νμ μ΄λ κ°μ, Return Type μ μμ λ‘κ² μ μν μ μλ€.
μ¬μ§μ΄ ν¨μμ λ§μ°¬κ°μ§λ‘ Variadic Parameters μ Default Parameter Values μμ κ°λ₯νλ€.λ¨, In-Out Parameters λ μ¬μ©ν μ μλ€.
struct Matrix {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(repeating: 0.0, count: rows * columns)
}
func indexIsValid(row: Int, column: Int) -> Bool {
row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> Double {
get {
assert(indexIsValid(row: row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValid(row: row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
- grid : μΌμͺ½ μλ¨μμ μμν΄ μ€λ₯Έμͺ½ νλ¨μΌλ‘ μ§νλλ νλ ¬μ νλ©΄ν λ²μ .
- indexIsValid : row μ column μ΄ μ ν¨ λ²μμΈμ§λ₯Ό νκ°.
- subscript get : κ°μ μ ν¨μ± κ²μ¬λ₯Ό μν΄ assertion μ ν¬ν¨. grid μ κ°μ μ°Ύμ λ°ν.
- subscript set : κ°μ μ ν¨μ± κ²μ¬λ₯Ό μν΄ assertion μ ν¬ν¨. grid μ κ°μ μ μ₯.
var matrix = Matrix(rows: 2, columns: 2)
print(matrix) // Matrix(rows: 2, columns: 2, grid: [0.0, 0.0, 0.0, 0.0])
μμ±λ grid λ₯Ό μκ°ν νλ©΄ μλμ κ°λ€.
Subscripts λ₯Ό μ΄μ©ν΄ κ°μ μ μ₯ν΄λ³΄μ.
matrix[0, 1] = 1.5
matrix[1, 0] = 3.2
μ ν¨νμ§ μμ κ°μ μ μ₯νλ € ν κ²½μ° assertion μ μν΄ μλμ κ°μ΄ μλ¬κ° λ°μλλ€.
matrix[2, 0] = 3.2 // __lldb_expr_13/1. Subscripts.xcplaygroundpage:45: Assertion failed: Index out of range
μ΄μ μμμ μ μ₯ν λ°μ΄ν°λ₯Ό Subscripts λ₯Ό μ΄μ©ν΄ μ‘°νν΄λ³΄μ.
print(matrix[1, 0]) // 3.2
2. Type Subscripts
Subscripts μμ Properties, Methods μ λ§μ°¬κ°μ§λ‘ Instance λΏλ§ μλλΌ Type
μ체μ
Subscripts
λ₯Ό μ μν μ μλ€.
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
static subscript(n: Int) -> Planet {
Planet(rawValue: n)!
}
}
let earth = Planet(rawValue: 3)!
print(earth) // earth
let mars = Planet[4]
print(mars) // mars
- Initializers λ₯Ό μ΄μ©ν΄ new Instance βearthβ λ₯Ό μμ±νλ€.
- Subscripts λ₯Ό μ΄μ©ν΄ getterκ° new Instance βmarsβ λ₯Ό μμ±νλ€.
print(type(of: earth)) // Planet
print(type(of: mars)) // Planet
κ²°κ³Όλ¬Όμ λμΌνμ§λ§,
Subscripts
λ₯Ό μ΄μ©νλ©΄ 맀λ²Initializers
λ₯Ό μ¬μ©ν νμ μμ΄Arrays
λDictionaries
μ μ κ·Όνλ―new Instance
λ₯Ό μμ±ν μ μλ€.
Reference
- βSubscripts.β The Swift Programming Language Swift 5.7. accessed Nov. 28, 2022, Swift Docs Chapter 11 - Subscripts.