iOS Swift5 语法

Swift语法 Swift5 【10 - 下标】

2020-05-08  本文已影响0人  Liwx

iOS Swift 语法 底层原理内存管理分析 专题:【iOS Swift5语法】

00 - 汇编
01 - 基础语法
02 - 流程控制
03 - 函数
04 - 枚举
05 - 可选项
06 - 结构体和类
07 - 闭包
08 - 属性
09 - 方法
10 - 下标
11 - 继承
12 - 初始化器init
13 - 可选项


目录


01-下标(subscript)

class Point {
    var x = 0.0, y = 0.0
    subscript(index: Int) -> Double {
        set {
            if index == 0 {
                x = newValue
            } else if index == 1 {
                y = newValue
            }
        }
        get {
            if index == 0 {
                return x
            } else if index == 1 {
                return y
            }
            return 0
        }
    }
}

var p = Point()
p[0] = 11.1
p[1] = 22.2

print(p.x)  // 11.1
print(p.y)  // 22.2
print(p[0]) // 11.1
print(p[1]) // 22.2


02-下标的细节

class Point {
    var x = 1.0, y = 2.0
    subscript(index: Int) -> Double {
        get {
            if index == 0 {
                return x
            } else if index == 1 {
                return y
            }
            return 0
        }
    }
}

let p = Point()
//p[0] = 10   // 没有set方法不能修改, error: cannot assign through subscript: subscript is get-only
print(p[0])

class Point {
    var x = 1.0, y = 2.0
    subscript(index: Int) -> Double {
        if index == 0 {
            return x
        } else if index == 1 {
            return y
        }
        return 0
    }
}


let p = Point()
//p[0] = 10   // 没有set方法不能修改, error: cannot assign through subscript: subscript is get-only
print(p[0])

class Point {
    var x = 1.0, y = 2.0
    subscript(index i: Int) -> Double {
        if i == 0 {
            return x
        } else if i == 1 {
            return y
        }
        return 0
    }
}

let p = Point()
p.y = 22.2
//print(p[1])     // 如果有标签,必须加上标签 error: missing argument label 'index:' in subscript
print(p[index: 1])  // 22.2

class Sum {
    static subscript(v1: Int, v2: Int) -> Int {
        return v1 + v2
    }
}
print(Sum[10, 20])  // 30

03-结构体、类作为返回值对比

class Point {
    var x = 0, y = 0
}

class PointManager {
    var point = Point()
    subscript(index: Int) -> Point {
        get { point }
    }
}

var pm = PointManager()
pm[0].x = 11
pm[0].y = 22
print(pm[0].x, pm[0].y) // 11 22

struct Point {
    var x = 0, y = 0
}

class PointManager {
    var point = Point()
    subscript(index: Int) -> Point {
        set { point = newValue }  // set方法
        get { point }
    }
}

var pm = PointManager()
pm[0].x = 11    // 如果没有set方法, Cannot assign to property: subscript is get-only
// pm[0].x = 11 本质是 pm[0]= Point(x: 11, y: pm[0].y)
pm[0].y = 22
print(pm[0].x, pm[0].y) // 11 22

04-接收多个参数的下标

class Grid {
    var data = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
    ]
    
    subscript(row: Int, column: Int) -> Int {
        set {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return
            }
            data[row][column] = newValue
        }
        get {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return 0
            }
            return data[row][column]
        }
    }
}

var grid = Grid()
grid[0, 1] = 77
grid[1, 2] = 88
grid[2, 0] = 99
print(grid.data)

05-下标默认参数

class Grid {
    var data = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]
    ]
    // column 设置默认参数为0
    subscript(row: Int, column: Int = 0) -> Int {
        set {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return
            }
            data[row][column] = newValue
        }
        get {
            guard row >= 0 && row < 3 && column >= 0 && column < 3 else {
                return 0
            }
            return data[row][column]
        }
    }
}

var grid = Grid()
grid[0, 1] = 77
grid[1, 2] = 88
grid[2, 0] = 99
print(grid.data)
grid[1] // 3, 相当于grid[1, 0]

iOS Swift 语法 底层原理内存管理分析 专题:【iOS Swift5语法】

下一篇: 11 - 继承
上一篇: 09 - 方法


上一篇下一篇

猜你喜欢

热点阅读