使用swift开发ios的时候把业务可以写入到属性观察器,我也挺喜欢叫钩子函数
var currentIndex:Int?{
didSet{
if currentIndex != 3{
titleViewCells![currentIndex!].focusCell()
titleViewCells!.filter{$0.data?.index != currentIndex}.forEach{$0.blurCell()}
if let scrollview = scrollerContainer {
//滚动,切换子控制器
var offset = scrollview.contentOffset
offset.x = CGFloat(currentIndex!) * scrollview.width
scrollview.setContentOffset(offset, animated: true)
}
}else{
forwardPage()
}
}
}
currentIndex是子控制器容器的索引,他的更新会引起子控制器的切换,其实这个实现完全可以在属性setter函数重写(重载也可以)
class People(name:String,about:String){
type observer = (Int) => Unit
private var privateAge = 0
def age = {privateAge}
def age_(willSet:observer=(newage:Int)=>{println(s"willSet ${newage}")},
newValue: Int,
didSet:observer=(newage:Int)=>{println(s"didSet ${newage}")}):Unit= {
willSet(newValue);
privateAge = newValue
didSet(newValue);
}
}
object KVOObject{
def main(args: Array[String]): Unit = {
val p = new People("twp","programer")
p.age_(
(newage:Int)=>{
println(s"newage is ${newage} and willSet")
println(s"age oldval is ${p.age}")
},
26,
(newage:Int)=>{
println(s"newage is ${newage} and didSet")
println(s"age is ${p.age}")
})
p.age_(newValue = 26) }
}
这来实现组装的回调来编写业务处理还是很方便的。(第一次写简书,多多见谅)