属性观察器, willSet和didSet的使用

26. January 2016 Swift 0

var maxValue = 999

var mineValue = -999

var number = 0 {

willSet{

print(“将从\(number)变成\(newValue)”)

}

didSet{

if number > maxValue{

number = maxValue

}else if number < mineValue {

number = mineValue

}

print(“已经从\(oldValue)变成\(number)”)

}

}

number = 1000

number


Leave a Reply

Your email address will not be published. Required fields are marked *