温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用swift类方法

发布时间:2021-10-13 14:40:38 来源:亿速云 阅读:128 作者:iii 栏目:编程语言

本篇内容介绍了“如何使用swift类方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

类的实例方法

//类的实例方法
class Counter {
    var count = 0
    
    func increment() {
        count += 1
    }
    
    func increment(by count: Int) {
        self.count += count
    }
    
    func reset() {
        count = 0
    }
}

let counter = Counter()
print(counter.count)
//0
counter.increment()
print(counter.count)
//1
counter.increment(by: 10)
print(counter.count)
//11
counter.reset()
print(counter.count)
//0

结构体修改属性 mutating

//结构体修改属性 mutating
struct Point {
    var x = 0.0, y = 0.0
    mutating func moveBy(x deltaX: Double, y deltaY: Double) {
        x += deltaX
        y += deltaY
    }
}
var point = Point(x: 1.0, y: 1.0)
point.moveBy(x: 2.0, y: 2.0)
print(point)
//Point(x: 3.0, y: 3.0)

枚举修改属性 mutating

//枚举修改属性 mutating
enum TriStateSwitch {
    case off, low, high
    mutating func next() {
        switch self {
        case .off:
            self = .low
        case .low:
            self = .high
        case .high:
            self = .off
        }
    }
}

var ovenLight = TriStateSwitch.low
print(ovenLight)
//low
ovenLight.next()
print(ovenLight)
//high
ovenLight.next()
print(ovenLight)
//off

类方法

//类方法
class Tool {
    static func getSize() -> (Int, Int) {
        return (100, 100)
    }
}

print(Tool.getSize())
//(100, 100)

结构体用下标访问 subscript

//结构体用下标访问 subscript
struct TimesTable {
    let multiplier: Int
    subscript(index: Int) -> Int {
        return multiplier * index
    }
}
let threeTimesTable = TimesTable(multiplier: 3)
print(threeTimesTable[4])
//12

类用下标访问 subscript

//类用下标访问 subscript
class Matrix {
    let rows: Int, cols: Int
    
    var grid: [Double]
    
    init(rows: Int, cols: Int) {
        self.rows = rows
        self.cols = cols
        grid = Array(repeating: 0.0, count: rows * cols)
    }
    
    func indexIsValid(row: Int, col: Int) -> Bool {
        return row >= 0 && row < rows && col >= 0 && col < cols
    }
    
    subscript(row: Int, col: Int) -> Double {
        get {
            assert(indexIsValid(row: row, col: col), "Index out of range")
            return grid[(row * cols) + col]
        }
        
        set {
            assert(indexIsValid(row: row, col: col), "Index out of range")
            grid[(row * cols) + col] = newValue
        }
    }
}

var matrix = Matrix(rows: 2, cols: 2)
print(matrix.grid)
//[0.0, 0.0, 0.0, 0.0]
matrix[0, 0] = 1.0
matrix[0, 1] = 2.0
matrix[1, 0] = 3.0
matrix[1, 1] = 4.0
print(matrix.grid)
//[1.0, 2.0, 3.0, 4.0]

枚举用下标访问 subscript

//枚举用下标访问 subscript
enum Direction: Int {
    case top, right, bottom, left
    static subscript(n: Int) -> Direction {
        return Direction(rawValue: n)!
    }
}
let right = Direction[1]
print(right)
//right

“如何使用swift类方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI