美文网首页
swift 属性专题

swift 属性专题

作者: 帅气的阿斌 | 来源:发表于2020-04-15 12:27 被阅读0次

计算属性 简写设置器 在swift中setter/getter方法的实现

//计算属性 简写设置器
func propertyTest(){
    //自己实现一个frame 然后通过center属性获取中心值
    struct BinFrame{
        var x: CGFloat
        var y: CGFloat
        var width: CGFloat
        var height: CGFloat
        //这里读代码断句为 声明 类型为CGPoint 的 center 变量,{}代码块
        var center: CGPoint {
            
            get {
                return CGPoint(x: x + width*0.5, y: y + height*0.5)
                //get 会隐式返回表达式值 以下省略return也可
                //CGPoint(x: x + width*0.5, y: y + height*0.5)
            }
            
            //set显示声明参数
    //        set (newCenter){
    //            x = newCenter.x - width*0.5
    //            y = newCenter.y -  height*0.5
    //        }
            //或者省略 则默认参数为newValue
            set {
                x = newValue.x - width*0.5
                y = newValue.y -  height*0.5
            }
        }
        
        //只读属性的简写 默认为get
        var center2: CGPoint {
            //省略return
            //CGPoint(x: x + width*0.5, y: y + height*0.5)
            return CGPoint(x: x + width*0.5, y: y + height*0.5)
        }
        
    }

    //只读属性 不写set只有get即为只读属性

    var binframe = BinFrame(x: 100, y: 100, width: 50, height: 50)
    //通过get自动计算center值
    print(binframe.center)
    //通过set自动设置 x,y,width,height
    binframe.center = CGPoint(x: 20, y: 20)
    print(binframe.center)
    binframe.center2
    
    //类对象同样适用 以下为简单样例
    class Frame2{
        let a = 10
        let b = 11
        var c: Int {
            return a + b
        }
    }
    
    let frame2 = Frame2()
    print(frame2.c)
    
}
propertyTest()

属性观察者

// 属性观察者
func observer(){
    //struct Example
    class Example{
        var name: String = "1" {
            //willSet(newSetValue)或者适用默认
            willSet {
                print("will set \(newValue)")
            }
            
            //didSet(newDidsetValue)或者适用默认
            didSet {
                print("didSet \(oldValue)")
            }
        }
    }
    
    var example = Example()
    example.name = "2"
    print(example)
}
observer()

属性包装 属性包装映射值

//属性包装 概念:类似于注解 被属性包装修饰的属性的getter/setter方法由 属性包装 wrappedValue 值提供的
//本质:使被修饰的属性 在被读取和写入时,调用的是属性修饰器的 setter/getter 方法
func testPropertyWrapper() {
    //属性包装
    @propertyWrapper
    struct A {
        var name: String
        
        //映射值 固定为 projectedValue ,通过 $+被属性包装的属性名称获取值,被该属性包装的属性即会被赋予一个映射值
        //映射值本质:就是为被修饰的属性再添加一个值,并且这个值通过 $+被属性包装的属性名称获取值
        var projectedValue = "作为映射值"
        
        //属性包装必须含有 wrappedValue 属性
        var wrappedValue: String {
            get {
                print("get")
                return name
            }

            set {
                name = newValue
                //通过set同步修改映射值
                projectedValue = "映射值==\(newValue)"
                print("set")
            }
        }
        
        init(wrappedValue: String) {
            print("调用初始化方法 init(wrappedValue:")
            name = wrappedValue
        }
        
        init() {
            name = "初始化值"
            print("调用初始化方法 init")
        }
    }

    //属性包装的属性初始值问题
    struct B {
        //调用初始化init(wrappedValue:)
        @A(wrappedValue: "initValue")
        var bName

        //调用初始化init(wrappedValue:)
        @A(wrappedValue: "initB2Name")
        var b2Name

        //调用初始化init
        @A()
        var cName
        
        //当你为属性指定一个初始值时,Swift 默认使用 init(wrappedValue:) 初始化器来设置包装,所以用该类型包装必须要实现 init(wrappedValue:)
        @A
        var c2Name = "22"
        
        @A var mapValue: String
    }

    var b = B()
    b.bName//调用的是属性包装器的get方法
    b.bName = "setValue"//调用的是属性包装器的set方法
    
    b.cName = "cName"
    print(b.$cName)
    
    b.mapValue = "1"
    print(b.$mapValue)

    //属性包装实现原理 (本质:使被修饰的属性 在被读取和写入时,调用的是属性修饰器的 setter/getter 方法)
    struct C {
        private var _cName = A()
        var cName: String {
            set {
                _cName.wrappedValue = newValue
            }

            get {
                return _cName.wrappedValue
            }
        }
    }
    
}

testPropertyWrapper()

相关文章

网友评论

      本文标题:swift 属性专题

      本文链接:https://www.haomeiwen.com/subject/ntqfactx.html