Kvc:key value Coding (key value encoding)
In iOS development, KVC is a technology that we often use. So what does KVC do? Simply list the following:
- Value and Assignment (basic in development)
- Gets the value of the private variable of the object. (often used, such as uipagecontorl pagination, set dots as pictures)
- Changing the value of an object's private variable (often used)
- Simple dictionary turn model (occasionally used)
- Model Turn Dictionary
- Batch Fetch value
The lower level implementation of KVC dictionary to model
Usually when we manually turn a dictionary into a model, a class method is provided in the model to receive a dictionary, in which the dictionary is converted into a model, and the transformed model is returned.
+ (instancetype)statusWithDict:(NSDictionary *)dict{ Status *status = [[self alloc] init]; //利用KVC字典转模型 [status setValuesForKeysWithDictionary:dict]; return status;}
-
Analyze the underlying implementation principle of [status Setvaluesforkeyswithdictionary:dict]
< Code class= "Objectivec" >+ (instancetype) statuswithdict: (nsdictionary *) dict{Status * status = [[self alloc] init]; //using KVC dictionary to model //[status setvaluesforkeyswithdictionary:dict ]; //setvaluesforkeyswithdictionary: Principle-traverse all keys in the dictionary, go to the model to find the corresponding attribute, assign the value to the model attribute [Dict enumeratekeysandobjectsusingblock:^ (id _nonnull key, id _ Nonnull obj, bool * _nonnull stop) {//This line of code is the real property assignment to the model [status Setvalue:obj Forkey:key]; }]; return status;}
- KVC dictionary to model cons: You must ensure that the properties in the model correspond to key one by one in the dictionary. If it is not one by one corresponding, will be error, look at the wrong information,
[<Status 0x7fd439d20a60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key source. is the system call Setvalue:forundefinedkey: error. If you want to work around this problem, simply rewrite the object's SetValue in the model: Forundefinedkey:, the System method is covered, you can continue to use KVC, dictionary to model.- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
A little KVC.
setValue:forKey:Principle of Method Assignment
- First will go to the model to find there is no corresponding key setter method, there is a direct call set Method method assignment.
- In the previous step, go to the model to find a property with the same name as key, or assign to a property with the same name as key.
- If the previous step is not yet available, go to the attribute to find out if there is an underlined property with the same name as key, and some words are directly assigned.
- If not, then call the object directly Setvalue:forundefinedkey: Direct error
Wen/Li Xiaonan (Jane book author)
Original link: http://www.jianshu.com/p/a22ef43424f6
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".
The underlying implementation of the iOS KVC dictionary to model