Learn--plist files while practicing, lazy loading, model first Use--supplement Instancetype

Source: Internet
Author: User

I. What is a plist file

1> writing data directly into the code is not a reasonable practice. If the data is often modified, it is often necessary to turn over the corresponding code to modify, resulting in low code extensibility

2> as a result, you can consider often becoming data stored in a file, the program starts to read the latest data from the file. If you want to change the data, modify the data file directly, without modifying the code

3> can generally use a property list file to store data such as Nsarray or nsdictionary, which has a "property list file" extension of plist and therefore becomes "plist file"

Ii. Creating a plist file

Iii. parsing plist files

code example:

1     //a NSBundle object corresponding to a resource bundle (Pictures, audio, video, Plis, etc.)2     //nsbundle function: Used to access the corresponding resource bundle within the file, can be used to obtain the full path of the file3     //the resources added to the project will be added to the Master resource bundle4     //[NSBundle Mainbundle] is associated with the project's Master resource bundle5NSBundle *bundle =[NSBundle Mainbundle];6NSString *file = [Bundle Pathforresource:@"things"OfType:@"plist"];7Self.things = [Nsarray arraywithcontentsoffile:file];

Iv. use of plist documents note

The file name of 1>plist is not called "info" or "info".

2> add plist and other file resources, be sure to tick the following options

If you have forgotten to check the situation, you can use the way to solve

V. Lazy loading

1> Why use lazy to load

Loading plist and other file data, loading data with lazy loading, if one comes up all the data will be loaded, the first image program performance, second, the loading of data this operation may not be able to use, wasting memory-lazy loading is the essence of rewriting getter method, that is, when the use of data to load!

2> code Instance--the data that was previously obtained in the Viewdidload method, is loaded regardless of whether it is used or not. The following method is used to load the

1 @property (Strong, nonatomic) Nsarray *things; // override the Get method to implement lazy loading

1 //rewrite the things get method to implement lazy loading2-(Nsarray *) Things3 {4     if(_things = =NULL) {5         //Load Master Resource bundle6NSBundle *bundle =[NSBundle Mainbundle];7         //get the full path of the things.plist through the master resource bundle8NSString *file = [Bundle Pathforresource:@"things"OfType:@"plist"];9         //get data through a full pathTen_things =[Nsarray arraywithcontentsoffile:file]; One     } A     return_things; -}

VI. model--Model substitution dictionary

The essence of 1> model is to inherit the NSObject class file directly.

The process of 2> dictionary to model is best encapsulated inside the model, the model's attributes correspond to which key value of the dictionary, the model is calculated, the external does not interfere, external calls the model construction method to obtain the model

The 3> model should provide a constructor method that can pass in the dictionary parameters, a class method, an object method

-(Instancetype) Initwithdict: (Nsdictionary *) dict; + (Instancetype) xxxwithdict: (nsdictionary *) dict;4> Code instance: Declaration part of the model:
1 //ShopModel.h2 //UIView Practice3 //4 //Created by admin on 16/3/5.5 //copyright©2016 Year Admin. All rights reserved.6 //Commodity Model7 8 #import<Foundation/Foundation.h>9 Ten @interfaceShopmodel:nsobject One /** Product Name*/ A@property (nonatomic,strong) NSString *name; - /** Product Picture*/ -@property (nonatomic,strong) NSString *icon; the  --(Instancetype) Initwithdic: (Nsdictionary *) dic; -+ (Instancetype) Shopmodelwithdic: (Nsdictionary *) dic; - @end

The implementation part of the model:

1 //SHOPMODEL.M2 //UIView Practice3 //4 //Created by admin on 16/3/5.5 //copyright©2016 Year Admin. All rights reserved.6 //7 8 #import "ShopModel.h"9 Ten @implementationShopmodel One  A-(Instancetype) Initwithdic: (Nsdictionary *) DiC - { -     if(self =[Super init]) the     { -Self.name = dic[@"name"]; -Self.icon = dic[@"icon"]; -     } +     returnSelf ; - } +  A+ (Instancetype) Shopmodelwithdic: (Nsdictionary *) DiC at { -     return[[Self alloc] initwithdic:dic]; - } - @end

5> Lazy loading + model

Before the lazy loading code, the resulting data is a collection of dictionaries, the following code is implemented in lazy loading process of the dictionary conversion model

1 //rewrite the things get method to implement lazy loading2-(Nsarray *) Things3 {4     if(_things = =NULL) {5         //Load Master Resource bundle6NSBundle *bundle =[NSBundle Mainbundle];7         //get the full path of the things.plist through the master resource bundle8NSString *file = [Bundle Pathforresource:@"things"OfType:@"plist"];9         //get data through a full pathTen_things =[Nsarray arraywithcontentsoffile:file]; One          A         //  idea: Get method of data , directly returns the collection of models, iterates through the dictionary array, converts the data of the dictionary into model data in lazy loading process  13  nsmutablearray *arrtemp = [N Smutablearray array]; //  defines a mutable array  14  for  (nsdictionary *dic in   _things) { 15   [arrtemp Addobject:[shopmodel shopmodelwithdic:dic];  16   17< /span> _things = arrtemp; //  18      } +     return_things; -}

Vii. Supplementary Instancetype

1>instancetype on a type representation, as with an ID, can represent any object type

2>instancetype can only be used on return value types and cannot be used as an ID on parameter types

One benefit of 3>instancetype than ID: The compiler detects the true type of instancetype

Learn--plist files while practicing, lazy loading, model first Use--supplement Instancetype

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.