The realization and difference between KVC and runtime of dictionary-to-model

Source: Internet
Author: User

We know that in the development, the dictionary to the model is a very common design pattern, when the number of elements in the dictionary is less, we can directly use Dic[key] to assign values to the properties of the model object, but when the number of elements in the dictionary is more, then the previous solution is not, So there is the design pattern of KVC (key value coding: Key-valued code) dictionary-to-model.

The design principle of KVC:

[Item setvalue:@ "Value" forkey:@ "Property"]:

1. First go to the model to find there is no setProperty, find, direct call assignment [self setproperty:@ "value"]

2. Go to the model to find there is no property attribute, have, direct access attribute Assignment property = value

3. Go to the model to find there is no _property attribute, have, direct access attribute Assignment _property = value

4. Can not find, will be directly error Setvalue:forundefinedkey: report not Found errors

But there are three conditions that we commonly use to achieve KVC:

1. You must ensure that the attributes defined in the model are greater than or equal to the number of keys in the dictionary.

2. The basic data types in the model cannot be converted.

3. The name of the property must be the same as the key, otherwise the related attribute will be missing error

The implementation mode of KVC is to take out the key value in the dictionary, to find the corresponding property in the model, then consider whether we can grab the attribute object in the model and find the corresponding key value in the dictionary. So it's time to think about the runtime mechanism runtime. We first get the property names of the model objects, add them to an array, and then iterate through the arrays to assign values to the property objects during the traversal. This is one of the differences between the KVC and runtime used to implement a dictionary-to-model. Below, we use code to implement the dictionary-to-model implementation under Runtime:


In the model classification to achieve the following methods


This place involves a two-level conversion problem, that is, to find the corresponding value in the dictionary, the value may be a dictionary, then need to re-model conversion, the code is implemented as follows:

Secondary conversions: Determine if value is a dictionary, and if so, the model that corresponds to the dictionary conversion layer

and the custom object needs to be converted.

if ([Value Iskindofclass:[nsdictionary class]] &&![ Ivartype hasprefix:@ "NS"]) {

Which model to convert into

Get class

Class Modelclass = nsclassfromstring (Ivartype);

Value = [Modelclass modelwithdict:value];

}



Wen/Syncurbug is not Herman (author of Jane's book)
Original link: http://www.jianshu.com/p/df1de1784b6d
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Dictionary to model KVC and runtime both implementations and differences

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.