11-Swift方法、下标

2020-12-16  本文已影响0人  一抹相思泪成雨

1.方法

class Car {
    static var count = 0
    init() {
        Car.count += 1
    }
    static func getCount() -> Int { count }
}

let c0 = Car()
let c1 = Car()
let c2 = Car()
print(Car.getCount()) // 3

2.mutating

struct Point {
    var x = 0.0, y = 0.0
    mutating func moveBy(deltaX: Double, deltaY: Double) {
        x += deltaX
        y += deltaY
        /// self = Point(x: x + deltaX, y: y + deltaY)
    }
}
enum StateSwich {
    case low, middle, high
    mutating func next() {
        switch self {
        case .low:
            self = .middle
        case .middle:
            self = .high
        case .high:
            self = .low
        }
    }
}

3.@discardableResult

struct Point1 {
   var x = 0.0, y = 0.0
   @discardableResult mutating
   func moveX(deltaX: Double) -> Double {
       x += deltaX
       return x
   }
}
var p = Point1()
p.moveX(deltaX: 10)

4.下标

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

5.下标的细节

class Point1 {
    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
        }
    }
}

- 如果只有get方法,可以省略get
class Point2 {
    var x = 0.0, y = 0.0
    subscript(index: Int) -> Double {
        if index == 0 {
            return x
        } else if index == 1 {
            return y
        }
        return 0
    }
}

6.下标的细节

/// 可以设置参数标签
class Point3 {
    var x = 0.0, y = 0.0
    subscript(index i: Int) -> Double {
        get {
            if i == 0 {
                return x
            } else if i == 1 {
                return y
            }
            return 0
        }
    }
}

var p3 = Point3()
p3.y = 22.2
print(p3[index: 1]) // 22.2

/// 下标可以是类型方法
class Sum {
    static subscript(v1: Int, v2: Int) -> Int {
        return  v1 + v2
    }
}
print(Sum[10, 20]) // 30

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

class Point4 {
    var x = 0, y = 0
}
class PointManager {
    var point = Point4()
    subscript(index: Int) -> Point4 {
        get { point }
    }
}

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

struct Point5 {
    var x = 0, y = 0
    subscript(index: Int) -> Int {
        return index
    }
}

class PointManager2 {
    var point = Point5()
    subscript(index: Int) -> Point5 {
        set { point = newValue }
        get { point }
    }
}

8.接收多个参数的下标

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)

上一篇 下一篇

猜你喜欢

热点阅读