工作生活

属性

2019-07-01  本文已影响0人  曹来东

属性

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

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

print(MemoryLayout<Circle>.stride)//8
print(MemoryLayout<Circle>.size)//8
var circle = Circle(radius: 5)

print(MemoryLayout.size(ofValue: circle))//8
print(MemoryLayout.stride(ofValue: circle))//8

print(circle.radius)//5.0
print(circle.diameter)//10.0

circle.diameter = 12
print(circle.radius)//6.0
print(circle.diameter)//12.0

计算属性

struct Circle {
    //存储属性
    var radius: Double
    //计算属性
    var diameter: Double {
        set (newDiameter){
            radius = newDiameter / 2
        }
        get {
           return radius * 2
        }
    }
}
struct Circle {
    //存储属性
    var radius: Double
    //计算属性
    var diameter: Double {
        get {
           return radius * 2
        }
    }
}
//只读计算属性简写
struct Circle {
    //存储属性
    var radius: Double
    //计算属性
    var diameter: Double { return radius * 2}
}

枚举rawValue原理

enum TestEnum: Int {
    case test1 = 1,test2 = 2,test3 = 3
    var rawValue: Int {
        switch self {
        case .test1:
            return 10
        case .test2:
            return 11
        case .test3:
            return 12
        }
    }
}
print(TestEnum.test3.rawValue)//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()
//打印结果
Person init
-----
Car init
Car is running!

延迟存储属性注意点

struct Point {
    var x = 0
    var y = 0
    lazy var z = 0
 
}

var p1 = Point()
print(p1.z)

let p2 = Point()
//Cannot use mutating getter on immutable value: 'p2' is a 'let' constant
//Change 'let' to 'var' to make it mutable
print(p2.z)

属性观察器

struct Circle {
    var radius: Double = {
        
        willSet {
            print("willSet",newValue)
        }
        didSet {
             print("didSet",oldValue, radius)
        }
    }
    
    init() {
        self.radius = 1.0
        print("Circle init!")
    }
}

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

全局变量和局部变量

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
    //willSet 11
    //didSet 10 11
}
test()

inout关键字


struct Shape {
    var width: Int
    var side: Int {
        willSet {
            print("willSet side",newValue)
        }
        didSet{
            print("didSet side",oldValue,side)
        }
    }
    
    var girth: Int {
        set {
            width = newValue / side
            print("setGirth girth",newValue)
        }
        
        get{
            print("getGirth girth")
            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)//修改了side,触发side: willSet didSet方法
s.show()//触发girth属性的 get.side 是willSet和didSet,不会被触发
print("------------")
test(&s.girth)
//getGirth 先获取girth值,触发get
//setGirth 20修改了girth,触发girth: set方法.
s.show()//触发girth get方法.getGirth 

//打印结果

getGirth girth
width=20, side = 4 ,girth = 80
------------
willSet side 20
didSet side 4 20
getGirth girth
width=20, side = 20 ,girth = 400
------------
getGirth girth
setGirth girth 20
getGirth girth
width=1, side = 20 ,girth = 20

inout本质总结

类型属性(Type Property)

  1. 实例属性(Instance Property):只能通过实例对象去访问
    存储实例属性(Stored Instance Property):存储在实例对象的内存中,每个实例对象都有一份
    计算实例属性(Computed Property)

  2. 类型属性(Type Property):只能通过类型去访问
    存储类型属性(Stored Type Property):整个运行过程中,就只有一份内存(类似全局变量)
    计算类型属性(Computed Type Property)

类型属性细节

单例模式

public class FileManager {
    public static let shared = FileManager()
    private init(){}
}


public class FileManager {
    public static let shared = {
        //...
        //...
        //...
        return FileManager()
    }()
    private init() {}
}
上一篇 下一篇

猜你喜欢

热点阅读