KVC and KVO in objective-C

Source: Internet
Author: User

AboutKVC (Key-value-coding)Key-value Encoding

1. Key-value encoding is a mechanism used to indirectly Access Object Attributes. Using this mechanism, you do not need to call the setter, Getter methods, or variable instances to access object attributes.

2. The key-value encoding method is declared in the informal protocol (Category) nskeycodingvalue of OC. The default implementation method is provided by nsobject.

3. Key-value encoding supports attributes with object values and the type and structure of pure values. Non-object parameters and return types are identified and automatically encapsulated/unblocked.

Next we will use KVC (Key-value-coding) InIf there is no getter or setter methodAccess variable attributes

As follows, there are no setter, Getter methods, and @ Property

. H file

 
# Import <Foundation/Foundation. h> @ interface dog: nsobject {nsstring * Name;} @ end

. M file

 
# Import "dog. H" @ implementation dog @ end

Main File

# Import <Foundation/Foundation. h> # import "dog. H "int main (INT argc, const char * argv []) {@ autoreleasepool {dog * dog = [[[DOG alloc] init]; [DOG setvalue: @ "Niang Greek" forkey: @ "name"]; // sets the key-Value Pair nsstring * name = [DOG valueforkey: @ "name"]; // nslog (@ "The dog's name is .... % @... ", name);} return 0 ;}

Print result:

2. When we see the above content, we may have a question: how can we access the attribute? We can access it through the path, and we can also look at the pure value.

Dog. h file

 
 
 
# Import <Foundation/Foundation. h> @ Class tooth; @ interface dog: nsobject {nsstring * Name; int age; tooth * tooth;} @ end

. M file of the Dog class

 
# Import "dog. H" @ implementation dog @ end

Tooth class. h file

# Import <Foundation/Foundation. h> @ interface tooth: nsobject {int num; nsstring * color;} @ end

Tooth. m

 
# Import "tooth. H" @ implementation tooth @ end

Main File

 
 

# Import <Foundation/Foundation. h> # import "dog. H "# import" tooth. H "int main (INT argc, const char * argv []) {@ autoreleasepool {dog * dog = [[[DOG alloc] init]; [DOG setvalue: @ "Niang Greek" forkeypath: @ "name"]; [DOG setvalue: @ 5 forkeypath: @ "Age"]; nsstring * name = [DOG valueforkey: @ "name"]; nsnumber * age = [DOG valueforkey: @ "Age"]; nslog (@ "The dog's name is .... % @... ", name); nslog (@" % @ ", age); tooth * tooth = [[tooth alloc] init]; [tooth setvalue: @ 10 forkey: @ "num"]; [tooth setvalue: @ "black" forkey: @ "color"]; [DOG setvalue: Tooth forkey: @ "tooth"]; nsnumber * toothnum = [DOG valueforkeypath: @ "tooth. num "]; nsstring * toothcolor = [DOG valueforkeypath: @" tooth. color "]; nslog (@" the dog's teeth num is % @, color is % @ ", toothnum, toothcolor ); // The following uses the path [DOG setvalue: @ 12 forkeypath: @ "tooth. num "]; [DOG setvalue: @" white "forkeypath: @" tooth. color "]; nsnumber * toothnum1 = [DOG valueforkeypath: @" tooth. num "]; nsstring * toothcolor1 = [DOG valueforkeypath: @" tooth. color "]; nslog (@" the dog's teeth num is % @, color is % @ ", toothnum1, toothcolor1); [tooth release]; [Dog release];} return 0 ;}

Print result:


Ii. KVO (key value observing) key value observation

Key-value observation is a notification mechanism that allows an object to obtain the changes of specific attributes of other objects.

KVO is mainly used for view interaction. For example, if some data on the interface changes and the display of the interface also changes, it is necessary to establish a correlation between data and the interface.


Related Article

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.