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.