10-下标

2023-04-09  本文已影响0人  二斤寂寞

下标(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

□ get方法的返回值类型 □ set方法中newValue的类型

下标的细节1

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

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

下标的细节2

class Point {
    var x = 0.0, y = 0.0
    subscript(index i: Int) -> Double {
        if i == 0 {
            return x
        } else if i == 1 {
            return y                                                         
        }                                                       
        return 0
    }                                                           
}
var p = Point()
p.y = 22.2
print(p[index: 1]) // 22.2
class Sum {
    static subscript(v1: Int, v2: Int) -> Int {
        return v1 + v2
    }
}
print(Sum[10, 20]) // 30

推导细节:

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

断点22 查看汇编分析:

image.png image.png

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

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
// Point(x: 11, y: 22)
print(pm[0])
// Point(x: 11, y: 22)
print(pm.point)

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

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

var pm = PointManager()         // 如果不写set方法是会报错的
pm[0].x = 11                    Cannot assign to property: subscript is get-only
pm[0].y = 22                    Cannot assign to property: subscript is get-only

pm[0].x = 11  等价于 pm[0] = Point(x: 11, y: pm[0].y)

返回值类型时候,如果是struct修饰,需要写set方法。如果是类类型修饰(修改指针指向空间),需要set方法。

接受多个参数的下标

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)
上一篇下一篇

猜你喜欢

热点阅读