---restore content starts---
It is very fast and easy to learn the swift language in the process of learning swift languages by the developers who are aware of or proficient in objective-c language, and who are proficient in mastering the language of Objective-c. I am a master of the OC language, and then began to study the swift. In the process of learning Swift language, the author suggests that the developer of OC Foundation, in the process of writing Swift code, write the code in OC, they compare each other, I believe you can find a quick way to learn swift language. Experienced, patient and talented developers, who believe they can use Swift to develop projects in a week or so. Which consumes time, also just individual places of learning, understand and attention.
This time I wrote about the use of the listener in Swift, the KVO, and I started by writing the OC code and then translating it into swift code. In the process of transformation found some problems, a certain amount of time to study, to think, to find information. One of the issues that you need to pay attention to in KVO in Swift (as the personal test concludes, the code is wrong, and you expect to communicate).
1, in the process of writing Swift KVO, it can not listen to the basic data type of the properties, if you want to listen to change it to nsnumber type, or other types, otherwise listen to the proxy method does not go.
2, in the process of writing Swift's kvo, the monitored attribute must be decorated with "dynamic", otherwise the agent method of listening does not go.
3, in the process of writing Swift's KVO, ensure that the listener and the listener are present simultaneously (considering their life cycle).
4, in the process of writing Swift's KVO, make sure to remove the observer and prevent the memory leak at last.
Here is the Code section
Viewcontroller.swift page
viewcontroller.swift//Swift monitoring usage////Created by Mac on 16/2/6.//copyright©2016 year ZY. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller {@IBOutlet weak var btn:uibutton! var _view1:newview!; var myobject:mynsobject!; var age:int!; Mycontext the role of the main is to distinguish the listener object, the specific role, the section of their own Internet access data private var mycontext = 0 override func viewdidload () {Super.viewdid Load () Btn.addtarget (Self, Action: "Buttonaction:", ForControlEvents:UIControlEvents.Touch Upinside); _view1 = Newview (Frame:cgrectmake (100,50,100,100)); _view1.backgroundcolor = Uicolor.orangecolor (); Self.view.addSubview (_VIEW1); MyObject = Mynsobject (); _view1.addobserver (Self, Forkeypath: "Age", Options:NSKeyValueObservingOptions.New, Context: &mycontext);//SE Lf.myObject.data = NSDate (); Self._view1.data = NSDate (); Age = 10; } func buttonaction (bTn:uibutton) {age = age + 10; Let num = NSNumber (integerliteral:age); Self._view1.age = num; } override Func Observevalueforkeypath (keypath:string, Ofobject object:anyobject?, change: [String:anyobject]?, context:unsafemutablepointer<void>) {} override Func Viewdiddisappear (Animated:bool ) {super.viewdiddisappear (animated); Remove Listener self.removeobserver (self, Forkeypath: "Age", Context: &mycontext); } }
Newview.swift page
newview.swift// Swift monitoring usage//// Created by Mac on 16/2/6.// copyright©2016 year ZY. All rights Reserved.//import Uikitclass Newview:uiview {//lazy load//lazy var Label:uilabel = {//let label = UILabel (Frame:cgrect (x:0, y:0, width:100, height:40)) //Return label//} () //annotations,,, when using listening, the basic data type, cannot be used as a listener Property object, and the dynamic Var age:nsnumber! must be modified with dynamic in front ; Dynamic var data = NSDate (); Override Init (frame:cgrect) { super.init (frame:frame); } Required init? (Coder Adecoder:nscoder) { FatalError ("Init (coder:) have not been implemented") }}
---restore content ends---
How to use KVO (listening) in Swift and the precautions