๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

[Swift] Subscript๋ž€?

by Fomagran ๐Ÿ’ป 2021. 11. 16.
728x90
๋ฐ˜์‘ํ˜•

 

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ํ•ด์‰ฌ ํ…Œ์ด๋ธ”(๋”•์…”๋„ˆ๋ฆฌ)์„ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ์ค‘์— a["A"] ์ด๋Ÿฐ ์‹์œผ๋กœ ๊ฐ’์„ ์ง€์ •ํ•˜๊ณ 

 

๊ฐ’์„ ๊บผ๋‚ด์˜ค๋Š”์ง€ ์ฐพ์•„๋ณด๋˜ ์ค‘์— subscript๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ธ€์„ ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Subscript

 

Swift ๊ณต์‹ ๋ฌธ์„œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค!

ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด ๊ทธ๋ฆฌ๊ณ  ์—ด๊ฑฐํ˜•์—์„œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ •์˜ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ž€ ์ฝœ๋ ‰์…˜, ๋ฆฌ์ŠคํŠธ, ์‹œํ€€์Šค ๋“ฑ ์ง‘ํ•ฉ์˜ ํŠน์ • ๋ฉค๋ฒ„ ์—˜๋ฆฌ๋จผํŠธ์— ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค. ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ถ”๊ฐ€์ ์ธ ๋ฉ”์†Œ๋“œ ์—†์ด ํŠน์ • ๊ฐ’์„ ํ• ๋‹น(assign)ํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ฌ ์ˆ˜(retrieve) ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด, ๋ฐฐ์—ด(Array) ์ธ์Šคํ„ด์Šค์˜ ํŠน์ • ์—˜๋ฆฌ๋จผํŠธ๋Š” someArray[index] ๋ฌธ๋ฒ•์œผ๋กœ, ์‚ฌ์ „(Dictionary) ์ธ์Šคํ„ด์Šค์˜ ํŠน์ • ์—˜๋ฆฌ๋จผํŠธ๋Š” someDictionary[key]๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜๋‚˜์˜ ํƒ€์ž…์— ์—ฌ๋Ÿฌ ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ  ์˜ค๋ฒ„๋กœ๋“œ(Overload)๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ๋‹จ์ผ ์ธ์ž ๊ฐ’์„ ๋„˜์–ด, ํ•„์š” ๋”ฐ๋ผ ๋ณต์ˆ˜ ์ธ์ž ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, ์ถ”๊ฐ€์ ์ธ ๋ฉ”์†Œ๋“œ ์—†์ด ๊ฐ’์„ ํ• ๋‹นํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.


Syntax

 

์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ ์„ ์–ธ ๋ฌธ๋ฒ•์€ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ์™€ ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.

 

์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ์™€ ๋‹ค๋ฅธ ์ ์€, ์„œ๋ธŒ์Šคํฌ๋ฆฝํŠธ๋Š” ์ฝ๊ณ -์“ฐ๊ธฐ(read-write) ํ˜น์€ ์ฝ๊ธฐ ์ „์šฉ(read only)๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

์ •์˜๋Š” ๊ณ„์‚ฐ๋œ ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹๊ณผ ๊ฐ™์ด setter, getter ๋ฐฉ์‹์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

 

(๋งŒ์•ฝ get,set์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™์œผ๋กœ ์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.)

 

์•„๋ž˜์™€ ๊ฐ™์ด ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํŠน์ • ๊ฐ’์„ ๋ฐ›๊ณ  get์„ ์ด์šฉํ•ด์„œ ํŠน์ • ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๊ณ  set์„ ํ†ตํ•ด ํŠน์ • ๊ฐ’์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

subscript(index: Int) -> Int {
    get {
        // ์ ์ ˆํ•œ ๋ฐ˜ํ™˜ ๊ฐ’
    }
    set(newValue) {
        // ์ ์ ˆํ•œ set ์•ก์…˜
    }
}

Usage

 

Read-Only

 

์•„๋ž˜์™€ ๊ฐ™์ด ๊ณฑ์…ˆ์„ ํ•ด์ฃผ๋Š” getter๋ฅผ ๋งŒ๋“ค์–ด์„œ ํŠน์ • ๊ฐ’์„ ๋„ฃ์—ˆ์„ ๋•Œ ์›ํ•˜๋Š” ๊ฐ’์„ ๊ณฑํ•ด์ฃผ์–ด ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
        return multiplier * index
    }
}
let threeTimesTable = TimesTable(multiplier: 3)
print("six times three is \(threeTimesTable[6])")
// "six times three is 18" ์ถœ๋ ฅ

 

Read & Write

 

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 {
        return 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
        }
    }
}

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€