티스토리 뷰
출처
서브스크립트 문법
subscript(index: Int) -> Int {
get {
// return an appropriate subscript value here
}
set(newValue) {
// perform a suitable setting action here
}
}
subscript(index: Int) -> Int {
// return an appropriate subscript value here
}
struct TimesTable {
let multiplier: Int
subscript(index: Int) -> Int {
return multiplier * index
}
}
let threeTimesTable = TimesTable(multiplier: 3)
print("six times three is \(threeTimesTable[6])") // 18
서브스크립트 옵션
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
}
}
}
var matrix = Matrix(rows: 2, columns: 2)
matrix[0, 1] = 1.5
matrix[1, 0] = 3.2
let someValue = matrix[1, 1]
'iOS 개발 > Swift' 카테고리의 다른 글
[Swift 3] 초기화 (Initialzer) (0) | 2017.04.15 |
---|---|
[Swift 3] 상속 (Inheritance) (0) | 2017.04.15 |
[Swift 3] 메소드 (Method) (0) | 2017.04.15 |
[Swift 3] 프로퍼티 (Property) (0) | 2017.04.15 |
[Swift 3] 클래스와 구조체 (1) | 2017.04.14 |
- Total
- Today
- Yesterday
- ios
- optional
- Swift 3.0
- NSManagedObject
- applicationWillResignActive
- coredata
- NSManagedObjectContext
- CGImage
- delegate
- RunLoop
- Swift3
- Swift 3
- dictionary
- HTTP
- CIImage
- 읽기 좋은 코드가 좋은 코드다
- Arc
- docker
- thread
- Swift
- Block
- workerThread
- EffectiveObjectiveC
- AWS
- Swfit
- UIView
- string
- NSManagedObjectModel
- 꺼내먹어요
- set
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |