微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift 属性和方法

一、当使用let声明为const的时候,不管是本身还是在特定的类或者结构体中的变量均不能再做修改

struct Matrix {
    let rows: Int
    let columns: Int
    var grid: [Double]
    
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(repeating: 0, count: rows * columns)
    }
}
let matrix = Matrix(rows: 2, columns: 3)
//matrix = Matrix(rows: 2, columns: 3) // Cannot assign to value: 'matrix' is a 'let' constant
//matrix.grid = Array(repeating: 0, count: 2 * 3) // Cannot assign to property: 'matrix' is a 'let' constant

 

二、getter、setter、willSet、didSet

1、当对一个属性定义了get或者get和set之后,该属性则为计算属性

在Matrix结构体中增加如下

    var tempCurIndex = 0
    var curIndex: Int {
        get {
            return tempCurIndex
        }
        set {
            tempCurIndex = newValue
        }
    }

这里的curIndex可以使用let么?答案是不可以的

因为使用let则说明:1、在构造函数之外不能设置值。2、每次读取到的值是一样的

但是这里有set,并且get没法确保每次返回的都是同一个

 

因为curIndex定义了计算属性,则curIndex本身是不可变的。所以如果定义了getter之后,就不能再定义对应的属性监视器

 

2、属性监视器willSet、didSet

属性监视器是在属性的值发生改变时候触发的;所以由1可知getter跟willSet和didSet是互斥的,如果有getter则不能再定义willSet和didSet

 

三、类型属性

类型属性在C++中是指静态成员变量,即成员变量用static修饰。swift也是类似可以使用static修饰

使用static来定义值类型的类型属性,使用class累定义类类型的属性

值类型的有:struct和enum

类类型的有:class

例如:

struct SomeStruct {
    static var storedType = "Some value"
    static var computedtype: Int {
//        return 10 // 可以使用直接范围的方式
        get {
            return 10
        }
        set {
            
        }
    }
}

enum SomeEnum {
    static var storedType = "Some value"
    static var computedtype: Int {
//        return 10 // 可以使用直接范围的方式
        get {
            return 10
        }
        set {
            
        }
    }
}

class SomeClass {
    static var storedType = "Some value" // 这里尝试使用class,发现报错  Class stored properties not supported in classes; did you mean 'static'?
    class var computedtype: Int { // 这里尝试使用static,发现也是没问题能够正常运行
//        return 10 // 可以使用直接范围的方式
        get {
            return 10
        }
        set {
            
        }
    }
}

 

四、值类型和类类型的方法

值类型跟类类型方法有个不一样的地方是:值类型的方法不能修改值类型的属性,如果要修改的话则需要给方法增加mutating

    mutating func updateTempIndex(newValue: Int) { // 当把mutating去掉则报错 Cannot assign to property: 'self' is immutable
        tempCurIndex = newValue
    }

 

同理方法属性一样也有对应类型方法,即在方法前面增加static

 

五、附属脚本

当我们使用array或者Dict的时候使用的是 A[index] 进行取值和赋值

我们也可以在类里面增加类似于array的取值方法

struct Matrix {
    let rows: Int
    let columns: Int
    var grid: [Double]
    
    init(rows: Int, columns: Int) {
        self.rows = rows
        self.columns = columns
        grid = Array(repeating: 0, count: rows * columns)
    }
    
    subscript(index: Int) -> Double {
        return grid[index] // 取值可以这样直接返回,也可以写成get和set的方法
    }
    
    subscript(row: Int, column: Int) -> Double { // subscript可以重载
        get {
            return grid[row * columns + column]
        }
        set {
            grid[row * columns + column] = newValue
        }
    }
}

var matrix = Matrix(rows: 2, columns: 3)
matrix[0, 1] = 1.5
print(matrix[0, 1]) // subscript(row: Int, column: Int)
print(matrix[1]) // subscript(index: Int)

 

原文地址:https://www.cnblogs.com/czwlinux/p/16151600.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐