Monkey original, welcome reprint. Reproduced please specify: Reproduced from COCOS2DER-CSDN, thank you!
Original address: http://blog.csdn.net/cocos2der/article/details/51917539
Today to others to tell a Observable implementation and use of the scene, combined with Observable-swift Github:https://github.com/slazyk/observable-swift said half a day seems not particularly clear, Therefore wrote a simple realization, narrated the next observable attribute monitoring mechanism.
//: Playground-noun:a place where people can playImportUIKitImportFoundation//MARK:-ObservableClass Observable<T>{//define block structureTypealias Observer=T -Void //Declare a block for the execution of data changes Private varObserver:observer? //Data changes, the Observer informs varVALUE:T {DIDSET {Observer?(value)} } init (_ V:t) {value=V} func Observe (Observer:observer?) { Self.Observer=Observer Observer?(Value)}}//MARK:-Peoplestruct Peoplemodel { LetFirstname:observable<String> LetLastname:observable<String>Init (firstName:String, LastName:String) { Self.FirstName=Observable (FirstName) Self.LastName=Observable (LastName)}}//MARK:-Test//Test1 LetPeople=Peoplemodel (firstName:"Sunny", LastName:"Liu") people.FirstName.Observe {NewValueinchPrint"FirstName changed: \ (newvalue)")}people.LastName.Observe {print ("LastName changed: \ ($)")}people.FirstName.Value= "Sunny2"People.LastName.Value= "LIU2"//Test2Class House { LetLablehousename=UILabel () init () {}varPeople:peoplemodel?{didset {people?.FirstName.observe{[unowned Self]In Self.lableHouseName.text = $}} }}
This seems easy to understand, O (∩_∩) o haha ~
Implementing the observable mechanism in swift