Introduction to KAC

Source: Internet
Author: User

Today I have studied KVC. Next I will talk about my understanding of KVC, which may be insufficient. I will correct it in the future.

First, we can see what KVC is doing with this knowledge point. In fact, we will find that KVC (key-value-coding), key-value Encoding
KVC is mainly used to indirectly access instance variables (assign values )...

Next, let's take a look at the official KVC documents provided by Apple:

/* Given a value and a key that identifies an attribute, set the value of the attribute. given an object and a key that identifies a to-one relationship, relate the object to the caller, unrelating the previusly related object if there was one. given a collection object and a key that identifies a to-specify relationship, relate the objects contained in the collection to the handler, unrelating previusly related objects if there were.


The default implementation of this method does the following:

1. searches the class of the specified ER for an accessor method whose name matches the pattern-set <key> :. if such a method is found the type of its parameter is checked. if the parameter type is not an object pointer type but the value is nil-setnilvalueforkey: is invoked. the default implementation of-setnilvalueforkey: raises an nsinvalidargumentexception, but you can override it in your application. otherwise, if the type of the method's parameter is an object pointer type the method is simply invoked with the value as the argument. if the type of the method's parameter is some other type the inverse of the nsnumber/nsvalue conversion done by-valueforkey: Is already med before the method is invoked.

2. otherwise (no accessor method is found), if the explorer's class '+ accessinstancevariablesdirectly method returns yes, searches the class of the specified ER for an instance variable whose name matches the pattern _ <key>, _ is <key>, <key>, or is <key>, in that order. if such an instance variable is found and its type is an object pointer type the value is retained and the result is set in the instance variable, after the instance variable's old value is first released. if the instance variable's type is some other type its value is set after the same sort of conversion from nsnumber or nsvalue as in step 1.

3. otherwise (no accessor method or instance variable is found), invokes-setvalue: forundefinedkey :. the default implementation of-setvalue: forundefinedkey: raises an nsundefinedkeyexception, but you can override it in your application.

What I mentioned above is:

Indirectly assign values to variables.

Setvalue: forkey:

Or

Setvalue: forkeypath:

Key is used to identify instance variables

Value corresponding to the instance variable

_ <Key>, _ is <key>, <key>, or is <key> is the instance variable in this format. If yes, the value is assigned directly, if not, an exception occurs.

There are two major exceptions. The above Apple documentation also provides solutions.

First, this method is called when the key is not of the object type and the value is set to nil. The default implementation of this method throws an exception. We can override this method, solve the problem that value is nil

-(Void) setnilvalueforkey :( nsstring *) Key;

-(Void) setnilvalueforkey :( nsstring *) Key

{

}

Second, this method is called when the key does not exist. The default Implementation of the system throws an exception. We can rewrite this method to solve the problem that the key does not exist.

-(Void) setvalue :( ID) value forundefinedkey :( nsstring *) Key;

-(Void) setvalue :( ID) value forundefinedkey :( nsstring *) Key

{

}


Then let's talk about

Setvalue: forkey:

Or

Setvalue: forkeypath:

And

Setvaluesforkeyswithdictionary:

Differences between the three methods,

Setvalue: forkey: it is used to indirectly assign values to a single instance variable in a class, such

[P setvalue: @ "Zhang San" forkey: @ "_ name"];

Setvalue: forkeypath: mainly indirectly refers to a single instance variable in a class. This instance variable is also a class, and the instance variable of this class is assigned a value. The following points indicate a class, instance variable. This instance variable is also a class, and then the instance variable of this class

[P setvalue: @ "Wow haha" forkeypath: @ "A. Name"];

Setvaluesforkeyswithdictionary: indirectly assigns values to all instance variables in a class.

[P setvaluesforkeyswithdictionary: @ {@ "name": @ "Zhang San", @ "Age": @ 19, @ "sex": @ "male"}];


Okay, that's all today. If you don't understand the above, you can check it with the following code ....


#import <Foundation/Foundation.h>#import "A.h"@interface Person : NSObject{    NSString * _name;    NSInteger _age;    A * _a;}- (instancetype)init;- (void)saiHi;- (void)setNilValueForKey:(NSString *)key;- (void)setValue:(id)value forUndefinedKey:(NSString *)key;

# Import "person. H "@ implementation person-(void) saihi {nslog (@" Hello this is % @ % LD years old ", _ name, _ age); [_ A printname];} -(instancetype) Init {self = [Super init]; If (Self) {_ A = [[A alloc] init];} return self ;} // when the key is not of the object type and the value is set to nil, this method is called. The default Implementation of the system throws an exception. We can override this method, solve the problem that the value is nil-(void) setnilvalueforkey :( nsstring *) Key {} // this method is called when the key does not exist. The default Implementation of the system throws an exception, we can override this method to solve the problem that the key does not exist-(void) setvalue :( ID) value forundefinedkey :( nsstring *) Key {}@ end

#import <Foundation/Foundation.h>@interface A : NSObject{    NSString * _name;    }- (void)printName;@end

#import "A.h"@implementation A- (void)printName{    NSLog(@"%@",_name);}

# Import <Foundation/Foundation. h> # import "person. H "int main (INT argc, const char * argv []) {// KVC Apple scripting language, indirectly assigning the variable person * P = [[person alloc] init]; [p setvalue: @ "Zhang San" forkey: @ "_ name"]; // [p valueforkey: @ ""]; [p setvalue: @ 34 forkey: @ "Age"]; [p setvalue: @ "male" forkey: @ "sex"]; // 1. setsex is preferred. // 2. _ sex // 3. _ issex // 4.sex // 5. issex // If NO, null, no exception is generated // you can rewrite to solve the exception // you can define an empty class and add attributes later, define a 10 thousand-class. // none of the above can be written [p setvaluesforkeyswithdictionary: @ {@ "name": @ "Zhang San", @ "Age": @ 19, @ "sex ": @ "male"}]; // [p setvalue: [A set] forkey: @ "A"] // setvalue: forkeypath can assign a value to the variable's variable [p setvalue: @ "Wow haha" forkeypath: @ ". name "]; // when there is no space for A, it is impossible to assign a value to A. // when space is opened for a, [p saihi] can be assigned through keypath; // If the processing is null, write the setnilvalue method and return 0 ;}



Send to customer

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.