Swift笔记 - 06.属性

Swift笔记 - 06.属性

存储属性与计算属性

Swift中跟实例相关的属性可以分为2大类

print (MemoryLayout<Circle>.stride) // 8

struct Circle {
    //1存储属性
    var radius: Double
    //1计算属性
    var diameter: Double {
        set {
            radius = newValue / 2
        }
        get {
            radius * 2
        }
    }
}

var circle = Circle(radius: 5)
print(circle.radius) // 5.0
print(circle.diameter) // 10.0

circle.diameter = 12
print(circle. radius) // 6.0
print(circle.diameter) // 12.0
enum TestEnum: Int{
    case testl = 1, test2 = 2, test3 = 3
    var rawValue:
            Int {
        switch self {
        case .testl:
            return 10
        case .test2:
            return 11
        case .test3:
            return 12
        }
    }
}
class Car {
    init() {
        print("Car init!")
    }
    func run() {
        print("Car is running!")
    }
}

class Person {
    lazy var car = Car()
    init() {
        print("Person init!")
    }
    func goOut() {
        car.run()
    }
}

let p = Person()
print("--------")
p.goOut()

/* out put:
Person init!
--------
Car init!
Car is running!
*/
class PhotoView {
    lazy var image: Image = {
        let ur = "https://www.520it.com/xx.png"
        let data = Data(url: url)
        return Image(data: data)
    }()
}
struct Point {
    var x = 0
    var y = 0
    lazy var z = 0
}
let p = Point()
print(p.z)

属性观察器

struct Circle {
    var radius: Double {
        willSet {
            print("willet", newValue)
        }
        didSet {
            print("didSet", oldValue, radius)
        }
    }

    init() {
        self.radius = 1.0
        print("Circle init!")
    }
}

// Circle init!
var circle = Circle()
// willet 10.5
// didSet 1.0 10.5
circle.radius = 10.5
// 10.5
print(circle.radius)

willSet会传递新值,默认叫newvalue
didSet会传递1日值,默认叫oldvalue
在初始化器中设置属性值不会触发wi11Set和didset

var num: Int {
    get {
        return 10
    }
    set {
        print("setNum", newValue)
    }
}
num = 11 // setNum 11
print(num) // 10

func test() {
    var age = 10 {
        willSet {
            print("willSet", newValue)
        }
        didSet {
            print("didSet", oldValue, age)
        }
    }
    age = 11
    // willet 11
    // didSet 10 11
}
test()

inout的本质

struct Shape {
    var width: Int
    var side: Int {
        willSet {
            print("willSetSide", newValue)
        }
        didSet {
            print("didsetside", oldValue, side)
        }
    }
    var girth: Int {
        set {
            width = newValue / side
            print("setGirth", newValue)
        }
        get {
            print("getGirth")
            return width * side
        }
    }

    func show() {
        print("width=\(width), side=\(side), girth=\(girth)")
    }
}

func test(_ num: inout Int) {
    num = 20
}

var s = Shape (width: 10, side: 4)
test(&s.width)
s.show()
print("---------")
test(&s.side)
s.show()
print("---------")
test(&s.girth)
s.show()

output:

getGirth
width=20, side=4, girth=80
---------
willSetSide 20
didSetSide 4 20
getGirth
width=20, side=20, girth=400
---------
getGirth
setGirth 20
getGirth
width=1, side=20, girth=20

类型属性

struct Car {
    static var count: Int = 0

    init() {
        Car.count += 1
    }
}

let c1 = Car()
let c2 = Car()
let c3 = Car()
print(Car.count) // 3

版权所有,转载请注明出处 luowei.github.io.