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
那如果想要使用willSet和DidSet该如何操作呢?
可以使用属性继承的方式来增加对应的willSet和DidSet
class TestWillSet { var _speed: Double = 0 var speed: Double { get { return _speed } set { _speed = newValue } } } class SubTestWillSet: TestWillSet { override var speed: Double { willSet { print("willSet", newValue) } didSet { print("didset", oldValue) } } } var st = SubTestWillSet() st.speed = 10 // willSet 10.0 // didset 0.0
三、类型属性
类型属性在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)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-04-15 Lua字符串匹配
2020-04-15 Lua面向对象
2020-04-15 Lua杂项