[IOS] Development-dictionary to model and KVC

Source: Internet
Author: User

Requirements one: To convert the dictionary to its own object, the object's property is the dictionary key value, directly using the key value to get the data there are two shortcomings, one is the key value is difficult to remember, and the second is directly using the key value Xcode will not have smart hints, it is easy to lead to unknown errors. When using the model, you can point out the required data directly through the point syntax to ensure that the data is accurate. The advantage of using the model is that it is convenient to store and transmit values, and it is not easy to make mistakes. The main thing is that this is also in line with our MVC and MVVM design patterns.

Note: To use the model can not be separated from KVC, if we pass from the outside of a dictionary, the dictionary has two keys corresponding to the value we need, the two values are "name", "age". To use KVC, be aware that the properties of the model must correspond to the key values in the dictionary. such as "name" in the dictionary, the model must have a property of name. So you can use KVC, KVC two ways: (the default we have two model properties, respectively, name and age, the passed dictionary defaults to dict)

1. SetValue (model properties, Forkey: Dictionary key value)

Eg:setvalue (Self.name, forkey:dict["name"])

* This method of assignment is cumbersome, and each time a key value is required, the method will be called again. The difference between this assignment and Self.name = dict["name" is not too big

2. Setvaluesforkeyswithdictionary (Incoming dictionary)

Eg:setvaluesforkeyswithdictionary (Dict)

* Direct completion of the assignment, in fact setvaluesforkeyswithdictionary (incoming dictionary), it is also called SetValue (model properties, Forkey: Dictionary key value), traversing the dictionary to assign values to the model properties. Therefore, using KVC requires that the properties of the model must drink the dictionary's key value of one by one corresponding.

Requirement Two: What if we just want to use some of the values in the dictionary? A big dictionary with dozens of keys in it, are we going to define dozens of properties in the model based on the key value? Of course, our program apes are very lazy, certainly do not define so many key values, at this time we simply rewrite the KVC the following method is OK:

3.setValue (Value:anyobject, Forundefinedkey key:string)

* This method will help us filter the key value we do not have, when the value is executed setvaluesforkeyswithdictionary (dict), we call this method when we monitor the property that we do not have the key value in the dictionary.

* There is no need to write any code in the rewrite, if you want to print which key value we can print in it if not.

Requirement Three: We know that many dictionaries have the key value "id". While "id" is a data type in our iOS, it's obviously not possible to define "ID" as a model attribute name. At this point we just need to define a property name to save the "id", of course, in order to be more read, more rigorous, we are generally defined as "id" is most appropriate. And we know that to use KVC, the key value corresponds to the model name one by one, but we just want to get "id". At this point need to be "demand one" and "demand two" together to see. When we get the "id", we use the passed-in dictionary to assign the value: (At this point we add a new property "id")

Eg:self.ID = dict["ID"]

* When this is assigned, the value corresponding to "ID" is taken out.

But we also want to get other value, at this time we use Setvaluesforkeyswithdictionary (dict) to complete the assignment of other properties. Of course, you also need to rewrite the SetValue (value:anyobject, Forundefinedkey key:string) method, because the dictionary has the key value "ID" ("id" in the dictionary, and "id" is the newly defined property of the model). The complete code is as follows:

Swift

Override Init () {

Super.init ()

Self.id = dict[@ "ID"];

Setvaluesforkeyswithdictionary (Dict)

}

Override Func SetValue (Value:anyobject, Forundefinedkey key:string) {}

Oc:

+ (Instancetype) testwithdict: (Nsdictionary *) dict{

Test *test = [[Test alloc]init];

Test.id = dict[@ "ID"];

[Test setvaluesforkeyswithdictionary:dict];

return test;

}

?

-(void) SetValue: (ID) value Forundefinedkey: (NSString *) key{

}

?

* Note that when using KVC in an object method, you must first Super.init, because you must first ensure that an object exists so that you can access the object's properties.

* Note that when using the object method, the "W" in "with" must be capitalized when naming the method name, otherwise it cannot be completed Super.init

eg:-(Instancetype) initwithdict: (Nsdictionary *) dict

Add: KVC is more powerful than just assigning a value to a model, KVC can also assign a value to a read-only property and change the private property. For example, Tabbar in Uitabbarcontroller is a read-only property and cannot be assigned a value for a read-only property with a generic assignment, but

SetValue (value,forkey: "Attribute name")

Eg:setvalue (value,forkey: "TabBar")

?

Summary: KVC is the abbreviation for Set Value coding, which is a mechanism for accessing class properties directly through the string's name (key). Instead of passing the setter (the set) and Getter (reader).

[IOS] Development-dictionary to model and KVC

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.