swift Property

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)





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值