Classes that use KVC and KVO in Swift must inherit from NSObject
KVC
- Key-value coding
- is the mechanism of 1 indirect access objects
- The value of key is the string of the property name, the returned value is any type and needs to be converted to the desired type
- KVC is mainly two methods
- (1) Set the corresponding property by key
- (2) Get the corresponding property by key
- Example
- Class testforkvc:nsobject{var hwccsdn = "Hello World"} var instance = TESTFORKVC () var value = Instance.valueforkey ("HWC CSDN ") as String instance.setvalue (" Hello HwC ", Forkey:" Hwccsdn ")
KVO
- Key-value Observing
- A mechanism built on the KVC
- The main function is to detect changes in object properties
- This is the 1 perfect mechanism that does not require the user to design a complex inspector pattern for themselves
- The property to be inspected is preceded by the dynamic keyword
- Example
1th step, add the Dynamic keyword to the properties of the object to be inspected
Class toobserver:nsobject{dynamic var hwcdate = NSDate () func updatedate () {hwcdate = NSDate ()}}
2nd, declare 1 global variables to identify which property is inspected.
private var mycontext = 0
3rd step, Addobserver in the class to be inspected, removeobserver in the destructor, rewrite Observervalueforkeypath
Class testforcsdn:uiviewcontroller{var testvariable = Toobserver () override Func Viewdidload () {super.viewdidload () Testvariable.addobserver (self,forkeypath: "Hwcdate", Options:. New,context:&mycontext)} deinit{testvariable.removeobserver (self,forkeypath: "HwcDate")} overfide Func Observevalueforkeypath (Keypath:string,ofobject:anyobject,change:[nsobject:anyobject],context: unsafemutablepointer<void>) {if (context = = &mycontext) {println ("Changed to: (change[ nskeyvaluechangenewkey]!)}}} In this way, changes can be detected in the function Observevalueforkeypath.Let's see how OC achieves Kvo and KVC.
1, KVC
- KVC (keyvaluecoding) "Key-value-code" is a mechanism for accessing class instance variables directly through the string's name (key), accessed through a setter, getter method.
- Access and settings for properties
- KVC can be used to access and set the value of an instance variable. Key is the property name
- Setting mode: [Self setvalue:aname forkey:@ "name"]
- Equivalent to self.name = AName;
- Access mode: astring = [self valueforkey:@ "name"]
- Equivalent to astring = Self.name;
2. KVO Observer
- KVO (keyvalueobserver) "Key-value-listener" defines a mechanism by which we receive a "notification" when the property value of an object changes. The Observer is more accurate
- NSObject provides a listening mechanism. All subclasses will be able to listen.
- KVO is implemented based on KVC. Implementing a listening Step
(1) Register the Listener object . Anobserver refers to the listener, KeyPath is to listen to the value of the property, and the context to facilitate the transfer of the data you need, it is a pointer type.
- -(void) Addobserver: (NSObject *) anobserver
- Forkeypath: (NSString *) keypath
- Options: (Nskeyvalueobservingoptio
- NS) options
- Context: (void *) context//(void*) is any pointer type
- The options are the listening option, which is the value of the dictionary that the Listener returns. There are two common options:
- Nskeyvalueobservingoptionnew refers to the returned dictionary that contains the new value.
- Nskeyvalueobservingoptionold refers to the returned dictionary containing the old value.
(2) Implement the Monitoring method。 The listening method is called automatically when the value of the property changes.
- -(void) Observevalueforkeypath: (NSString *) keypath
- Ofobject: (ID) object
- Change: (nsdictionary *) change
- Context: (void *) context
- Where object refers to the subject being monitored. Changes are stored in change data, such as pre-change data and changed data.
3. Notice
- Notifications are a design pattern in the iOS development framework, and the internal implementation mechanism is supported by the cocoa framework.
- Notifications are typically used for information passing between M, V, C. Like I set the app skin on the Settings page.
- M is the Modol model V is the View view C is the control controller.
- System notifications
//Registration Notice
- [[Nsnotificationcenter Defaultcenter] Addobserver:self
- Selector: @selector (didfinish:)//didfinish: Is the method name self (whose) and Didfinish: Ok method
- Name:mpmovieplayerplaybackdidfinishnotification
- Object:nil];
- Selector is a method name class that describes a class of classes SEL [email protected] (method name)
- The notification runs out to remove
//Remove Notifications
- [[Nsnotificationcenter Defaultcenter] Removeobserver:self
- Name:mpmovieplayerplaybackdidfinishnotification
- Object:nil];
Summarize
This article describes two of the more distinctive mechanisms in iOS development: KVC and KVO
KVC: It can be violent to get/set private properties of the class, along with powerful key-value paths to manipulate the properties of the array type
KVO: A change in the value of a property in a listener class
iOS development--kvo/kvc&oc and Swift article