swift Property

    xiaoxiao2021-12-14  17

    import Foundation

    // property: 

    //      1. Stored property : Class Struct

    //      2. Calculated property : Class Struct Enum

    //      3. Type property AKA static property

    //      4. Property observer  : Stored property

    struct Range {

        var start: Int

        let length: Int

    }

    class Zone {

        var start : Int = 0

        let length : Int = 3

    }

    var rangeA = Range(start : 1, length : 3)

    rangeA.start = 3

    //rangeA.length = 4

    let rangeB = Range(start: 2,length:2)

    let rangeBClass = Zone()

    rangeBClass.start = 4

    //  if you declare let instance, you can't modify var property for value type

    //  But you can modify it for reference type

    //  rangeB.start = 3

    // lazy stored property : 

    //      1. can only be var (constant must have value when initialization)

    //      2. calculated until been used not when initialized

    class FileLoader {

        //  imagine the initialization took a long time to load

        //  imagination code here

        var fileName = "filename" //not important, just make it more real

    }

    class DataLoader {

        var SourceData = [String]()

        lazy var SourceFile = FileLoader()

    }

    var someLoader = DataLoader()

    print(someLoader.SourceData)

    someLoader.SourceData.append("aaa")

    someLoader.SourceFile.fileName// SourceFile been initialized fist time

    // computed property

    struct Point{

        var x : Int

        var y : Int

    }

    struct Size{

        var width: Int

        var height: Int

    }

    struct Rect{

        var origin : Point

        var size : Size

        var center : Point{

            get{

                let center_x = origin.x + size.width/2

                let center_y = origin.y + size.height/2

                return Point(x:center_x,y:center_y)

            }

            set{ // remove set then the property is readonly

                origin.x = newValue.x - size.width/2

                origin.y = newValue.y - size.height/2

            }

        }

    }

    var alpha = Rect(origin: Point(x:0,y:0), size: Size(width: 10, height: 10))

    print(alpha.center.x)

    alpha.center = Point(x: 20,y:20)

    print(alpha.origin.x)

    // *******

    // Property Observer:

    //      1. willSet called before value stored

    //      2. didSet called after value stored

    struct PropertyObserve {

        var someInt : Int = 1

        var someOtherInt : Int {

            // get & set can't coexist with property observer

            //get {

            //    return someInt + 1

            //}

            //set {

            //    someInt = newValue - 1

            //}

            willSet{

                print("we are setting new value now: it is \((newValue))")

            }

            didSet{

                if someOtherInt > oldValue{

                    print("getting bigger")

                }else{

                    print("getting smaller")

                }

            }

        }

    }

    // the first time value been initialized it will not call willSet

    // because property observer only respond to value changes, not value init

    var testObject = PropertyObserve(someInt: 1, someOtherInt: 2)

    testObject.someOtherInt = 3

    // *********************

    // Type property also known as static property

    // for static property you mut give it a default value

    enum Woman {

        static var isaWoman : Bool = true

        static var braSize : Character {

            switch self.braSize {

            case "A":

                return "A"

            default:

                return "B"

            }

        }

    }

    struct Egg {

        static var isRound : Bool = true

    }

    class Car {

        static var wheel = 4

        var brand:String {

            set{

                //self.brand = "whatthefaak" // this will failjh

            }

            get{

               return "hooray"

            }

        }

        // put class instead of static make child class able to override

        class var canbeInHerit:Int{

            return 3

        }

    }

    var carA = Car()

    var carB = Car()

    //Car.brand = "I just change it"

    print(carA.brand)

    carA.brand = "asa"

    print("what")

    print(carA.brand)

    class Car {

        var brand:String {

            set{

                //self.brand = "whatthefaak"

                //set was used to set other property

            }

            get{

               return "hooray"

            }

        }

    }

    var carA = Car()

    var carB = Car()

    print(carA.brand)

    carA.brand = "asa"

    print("what")

    print(carA.brand)

    // example from the guide

    struct SoundChannel{

        static let maxVoice = 10

        static var maxMark : Int = 0

        var currentMark : Int {

            didSet{

                if currentMark > SoundChannel.maxVoice {

                    currentMark = SoundChannel.maxMark

                }

                if currentMark > SoundChannel.maxMark {

                    SoundChannel.maxMark = currentMark

                }

            }

        }

        

    }

    var leftChannel = SoundChannel(currentMark: -4)

    print(leftChannel.currentMark)

    转载请注明原文地址: https://ju.6miu.com/read-968894.html

    最新回复(0)