iOS Model Design---dictionary turn model

Source: Internet
Author: User






iOS model design


in IOS development, the term "model" is almost accompanied by the development career of every programmer. Before contacting the model, the small part in the development will often encounter logic confusion, unclear organization and so on. Next, the small series will lead you to learn about the important part of the MVC architecture --- Data Model building.

before we begin, we will elaborate on an example. The small part in the previous article (IOS UI design - nine Gongge layout) left behind a nine-Gongge UI design interface.


throughout the layout, the icons and names required for each button module are from the data file ( plist files). Next, let's look at how to encapsulate this data into a model, avoid reading directly from Viewcontroller , resulting in viewcontroler.m file jumbled.


1 Condition Preparation


Before starting to explain, we need to prepare the environment, the basic materials, files need to be built, the relevant source code can be directly to the small series to get Oh!


1.1 Related Materials



1.2 plist file

Let's take a look at the contents of the plist file, such as. It is the key file that corresponds to the icon and function. The corresponding file name can be obtained directly from the key value.


1.3 Layout files

This paper focuses on the encapsulation of the model, not the layout of the interface, the relevant layout can be referred to theIOS UI design - nine Gongge layout. The default code is already layout-finished.



2 Data references 


2.1 plist File loading

, we can see that theplist file is actually an array, each element of the array is a dictionary. So we define a _plist property in the Viewcontroller.m file , and then we use the [Nsarray Arraywithcontentsoffiles] method to initialize the _plist. Take the lazy load form with the following code:

@property (Nonatomic,strong) Nsarray *plist;

-(nsarray*) plist

{

if (_plist = = nil)

{

NSString Path = [nsbundlemainbundle]pathofresource:@ "appinfo.plist" oftype:@ "nil";

_plist=[Nsarrayarraywithcontentsoffiles:path];

}

return _plist;

}

2.2 Material references

After the plist file is loaded, A dictionary is stored in the member variable _plist, and the corresponding value can be taken out according to the dictionary's properties. as shown below.


2.3 Disadvantage Analysis

(1) Currently, a dictionary is stored directly in the member variable _plist. All data is processed in VIEWCONTROLLER.M , which can easily cause redundancy in the file.

(2) in the dictionary reading, if the property name is wrong, will cause an error, and the wrong location too much, inconvenient to troubleshoot.

(3) in the above example, the dictionary has only two properties. If there are multiple key values, the manual assignment is large and the probability of error is higher.



3 Model Encapsulation 


3.1 Establishment of the model

Model Building Idea: The current member variable _plist is stored directly in the dictionary, each time the dictionary loading needs to change the Viewcontroller file. What we need to do is to have a data modelin the member variable _plist, the loading of the plist file in the data model, so that the controller no longer directly manipulate the loading of the data.

(1) Data Model

To create a new class: AppInfo. In the header file, declare two properties in the dictionary. The relevant code is as follows:

(2)       Data reference

Next, let's make the member variable _plist is stored in the above AppInfo model, and no longer a dictionary. So,the getter method of the member variable _plist in theviewcontroller.m file is changed to the lazy loading form as follows:


After changing the _plist variable to the storage model, the fetch data no longer uses the dictionary's key name, but rather the model's getter method to get the specific data in the Plist file.


3.2 The difference between Instancetype and ID

So far, the model has been set up. But the attentive reader can find that although the dictionary is saved in the member variable, the code volume looks a lot more, still feels more chaotic, the data model is still not completely separated. Before we optimize it, let's take a look at a knowledge point.

after IOS7 ,Apple strongly recommends that we use a data type instancetype. This data type is similar to ID and is a universal pointer. But they still have a difference. The difference is mainly embodied in, class method,instancetype can dynamically distinguish the current object data type, compile detection, and ID is not.

Attention:

①instancetype can only be used for return values, not as arguments

The ②ID type is also used to dynamically detect object types in Object methods .

③ in subsequent development, both the object method and the class method, the initialization method uses the Instancetype pointer as much as possible .



3.3 Optimization of the model

Next, we enter the important link - the optimization of the model. The optimization of the model is mainly divided into the following parts:

① The initialization of the dictionary into the model file

② class methods are more efficient than object methods

③ take KVC encoding, Batch dictionary assignment

④ Place your code where it appears

3.3.1 Dictionary Initialization

In the Getter method of the member variable _plist , a model object AppInfo is defined andthen assigned a value. In fact, in the actual development process, in order to separate the relevant data operations, the assignment here can be directly put into the model processing, the optimized code is as follows:

declaring initialization methods in ① AppInfo.h files


② The method in a APPINFO.M file


③ Reference data in a VIEWCONTROLLER.M file


3.3.2 Changing class methods

In the previous summary, we defined the Initialize object method in the class. But in real-world development, experienced programmers often provide class methods for initialization operations. Object methods and class methods exist simultaneously, which is also a manifestation of programmer's professionalism. More importantly: class methods are more efficient.

① defining class methods in AppInfo.h files


② instantiating a class method in a APPINFO.M file


③ called in the viewcontroller.m file


Use of 3.3.3 KVC (key value encoding)

KVC (key-valuecoding), named "Key-value encoding." In this case, the assignment to the dictionary is taken manually by the attribute value of one by one corresponding, if the property is too much, the workload is very large. In addition, it is very error prone. Use setvaluesforkeyswithdictionary to automatically assign values. Note that the key name of the dictionary must be the same as the member variable name of the model. Modifies The initialization method in the APPINFO.M file.


3.3.4 Place the code where it appears

(1) where to load the plist file

plist as a data file, it should appear in the model. So we provide a class method in the model that is used to load the plist file. When the viewcontroller.m file needs to plist file, it is good to find the model file loading process directly.

① AppInfo.h file


② appinfo.m file


③ viewcontroller.m file



(2) UIImage is also encapsulated in the model

  in the preceding code, we get the name of the picture from the model, and then in theVIEWCONTROLLER.Mfiles are usedUIImageof theimagenamedmethod gets the picture by the name of the picture. However, in practice, we can stillImagealso encapsulated in the model,only need to be inVIEWCONTROLLER.Mfile to get it through the model.

① AppInfo.h file


② appinfo.m file

③ viewcontroller.m file




here, our model encapsulation is basically complete. The main idea of the model is to peel the data-related code into the model file, so that Viewcontroller can no longer manage the data access operation, etc., when the Controller needs the data, only need to call the model file to get.





iOS Model Design---dictionary turn model

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.