Lazy Load Dictionary to model

Source: Internet
Author: User

>>> lazy Loading One, introduction

Lazy loading is also called delayed loading, that is, when the system is called to load, if the system does not call will not be loaded, so-called lazy loading is actually rewriting its get method.

When using lazy loading, you should first determine if the method exists, and then instantiate it if it does not exist.

Second, the advantages

You do not have to write the methods for creating objects in Viewdidload, and the code is more readable.

Each control's Getter method is responsible for the respective instantiation process, which is independent and low-coupling.

Third, the use

Declares a property that can be either a private property or a property declared in the. h, and a lazy-loaded object is a property.

The viewdidload outside of the. m file overrides lazy loading, which overrides its getter method.

First determine if the _ object exists, if it does not exist, instantiate the Property object in if, and set the object's properties, and add its own method.

Returns an _ object.

>>> dictionary to model one, introduction

Dictionary data/arrays (which can be data in plist or data in the background of the network, etc.) are converted into model objects/arrays.

Model to provide a constructor that can pass in a dictionary parameter (an object is a class method)

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

+ (Instancetype) initwithdict: (Nsdictionary *) dict;

Second, the advantages

Encapsulating the data in a dictionary into a model class, with the advantages of the MVC framework idea, can reduce coupling.

All dictionary-to-model can be written in one place, easy to manage, and can reduce the error rate of code.

After the dictionary turns to the model, the data in the dictionary is transferred directly to the properties of the model, and the property of the model can be called directly to obtain the data, which can improve the coding efficiency of the code.

Because the model is in a separate class, the outside world does not care about the details of the class, as long as the use can, better reflect the idea of object-oriented.

Third, the use

> Declare all the properties required for the lock in the. h file of the model.

> Instantiate two methods in a model's. h file:

-(Instancetype) Initwithdict: (Nsdictionary *) dict;

+ (Instancetype) initwithdict: (Nsdictionary *) dict;

> in the model's. m file, get the data by implementing both methods:

-(Instancetype) Initwithdict: (Nsdictionary *) dict{

self = [super init];

if (self) {//Assign value to declared property

Self.name = dict[@ "name"];

Self.icon = dict[@ "icon"];

}

return self;

}

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

return [[Self alloc] initwithdict:dict];

}

> When lazy loading is judged to be empty, the data is passed to the model array by path path to create a temporary array that can be used to hold data, traverse the obtained data to assign values to the temporary array, and assign the temporary array to the model object.

  

  

Iv. Links

Http://www.cnblogs.com/ansyxpf/p/5494240.html

Lazy Load Dictionary to 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.