所以我所拥有的是一个看起来有点像这样的静态类
class thing{
static var something: Bool = false
static func update(completion: (()->Void)?){
completion()
}
//or
static func update(){}
}
然后我希望有其他基于视觉的类,将我的代码添加到静态更新函数的末尾
class view: UIView{
var square: UIView {
let temp = UIView()
temp.background = UIColor.red
return temp
}()
func setup(){
//this will be tacking code on the end of the update function
//so when ever that function is run this code will run as well
thing.update{
if (thing.something){
self.square.background = UIColor.blue
else{
self.square.background = UIColor.red
}
}
}
我的代码中的其他地方我想要更改静态变量,然后调用更新函数,该函数也会运行代码添加。让我想到这一点的是firebase实时数据库,他们如何创建他们的观察者。
let _ = FIRDatabase.database().reference().child("something")..observe(FIRDataEventType.value, with: {(snapshot) in
//run your tacked on code
})
如果数据库中有更改,则会运行此操作。这就是我想要重新创造的东西。