Kvc/kvo using details and invocation order

Source: Internet
Author: User

key-value Coding (KVC)key-value Coding (KVC)KVC, which means nskeyvaluecoding, an informal Protocol, provides a mechanism to indirectly access the properties of an object. KVO is one of the key technologies based on KVC implementation.  An object has some properties. For example, a person object has a name and an address property. In KVC parlance, the person object has a value corresponding to the key of his name and address. A key is just a string, and its corresponding value can be any type of object. At the most basic level, KVC has two methods: one is to set the value of key and the other is to get the value of key.   //Override setter Method and getter method:1. The Dog object has attributes:@property(nonatomic,copy)nsstring *name; 2. If both the setter and Getter methods are overridden: ///Remember: If you override both the setter and getter methods, the system does not automatically generate _NAME member variables.required @synthesize name = _name; to tell the system that you're going to help me build _name .3.[Mydog setValue:@ "mbxxx" forkey:@ "_name"]; @ "_name" @ "name" can be accessed to this member variable  4. nsstring *age = [Mydog valueforkey:@ "age"]; When this code is called,1) The system first to find the variable has no Getter method. If so, call the Getter method to take a value2) If there is no Getter method, the system looks for either an age member or a _age member. 5. If a global variable is declared only in the. m file:{
@public nsstring *age; }at this point, only the KVC mode can be used to assign values and values.//Note: First assign, then take. [Mydog setValue:@ "Ten" forkey:@ "age"];
nsstring *age = [Mydog valueforkey:@ "age"]; NSLog(@ "age = =%@", age); Print Result: Age = = =description, the assignment and the value process were successful. KVO:that is: Key-value observing, which provides a mechanism for the object to receive notification when the properties of the specified object have been modified. Simply put, the KVO will automatically notify the appropriate observer when the property of the observed object has been modified each time it is specified. DOG.M Code:#import "Dog.h" @implementation Dog
{
@public nsstring *age; }@synthesize name = _name; -(void) SetName: (nsstring *) name{ //1 NSLog(@ "xxxxx"); _name = name; }-(nsstring *) name{ //2
NSLog(@ "AAAAA");
return _name; }@end PERSON.M Code:#import"Person.h"
@implementationPerson

-(void) Setdog: (Dog*) dog{ //3
_dog= Dog;
//Add current object as observer
[_dog addobserver:self < Span data-mce-= "" >forkeypath:@ "name"  options:nskeyvalueobservingoptionnew |  nskeyvalueobservingoptionold context: @ "Hello kvo" ";
}
-(void _dog removeobserver :self forkeypath: @ "name" " }-(void) Observevalueforkeypath: (nsstring *) keypath ofobject: (ID) object change: ( Nsdictionary *) Change context: (void *) context{ //4 NSLog(@ "context==%@", context); if([Object iskindofclass: [Dog class]] && [keypath isequaltostring: c10>@ "name"]) { nsstring *new = change[nskeyvaluechangenewkey];
NSLog(@ "new = = =%@", New);
}
}@endWhat does the invocation process look like?person *p = [[person alloc]init];
P.Dog = mydog; Mydog. name = @ "Fyz"; //5 s) call p.Dog = Mydog; method, go to Method 3. 2) then come to Method 53) came to Method 2 (Getter)//printed AAAA4) then come to Method 1 (Setter)//print xxxx (why here? Need to test) 5) Come Back to Method 2 (Getter)//print AAAA (here again why? need to test) 6) Then come to Method 4//Print new = = =7) Last Go destructor method 6///Do NOT join observer mode, only one time Method 1 (Set method), equivalent to the existence of the observer pattern.A getter method is called first, then the setter method is adjusted once, then the getter method is adjusted once.

Kvc/kvo using details and invocation order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.