Three methods of dictionary-to-model four: summary

Source: Internet
Author: User

Model

1 //2 //YSFoodTypeModule.h3 //Ysuisplitviewcontroller4 //5 //Created by Ys on 15/12/12.6 //Copyright (c) 2015 Ys. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceYsfoodtypemodule:nsobject A  -@property (nonatomic,copy) NSString *Idstr; -  the@property (nonatomic,copy) NSString *name; - //If you use Mjextension, do not write the following two methods --(Instancetype) Initwithdict: (Nsdictionary *) dict; -  ++ (Instancetype) foodtypewithdict: (Nsdictionary *) dict; -  + @end

The. m file that corresponds to the model

1 //2 //YSFOODTYPEMODULE.M3 //Ysuisplitviewcontroller4 //5 //Created by Ys on 15/12/12.6 //Copyright (c) 2015 Ys. All rights reserved.7 //8 9 #import "YSFoodTypeModule.h"Ten  One @implementationYsfoodtypemodule A  -+ (Instancetype) foodtypewithdict: (Nsdictionary *) Dict - { the     return[[Self alloc]initwithdict:dict]; - } -  --(Instancetype) Initwithdict: (Nsdictionary *) Dict + { -     if(self =[Super Init]) { +          ASelf.name = dict[@"name"]; atSelf.idstr = dict[@"Idstr"]; - //        //or KVC, the disadvantage is that the dictionary key must have the corresponding attribute in the model, otherwise crashes, and mjextension can solve the problem - //[self setvaluesforkeyswithdictionary:dict]; -     } -     returnSelf ; - } in  - @end

Controller code

1 //2 //YSFOODTYPESTABLEVIEWCONTROLLER.M3 //Ysuisplitviewcontroller4 //5 //Created by Ys on 15/12/12.6 //Copyright (c) 2015 Ys. All rights reserved.7 //8 9 #import "YSFoodTypesTableViewController.h"Ten #import "YSFoodTypeModule.h" One #import "MJExtension.h" A  - @interfaceYsfoodtypestableviewcontroller () -  the@property (nonatomic,strong) Nsarray *foodtypes; -  - @end -  +  - @implementationYsfoodtypestableviewcontroller +  A-(Nsarray *) Foodtypes at { -     if(_foodtypes = =Nil) { -         //Here are some ways to turn a dictionary into a model -          -         //----0, call the dictionary-to-model method inside the model - //Nsarray *dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "food_types. Plist "Oftype:nil]"; in //Nsmutablearray *arraym = [Nsmutablearray array]; - //For (nsdictionary *dict in Dictarray) { to //ysfoodtypemodule *foodt = [Ysfoodtypemodule foodtypewithdict:dict]; + //[Arraym Addobject:foodt]; - //            } the //_foodtypes = Arraym; *          $         //----1, using the dictionary in Mjextension to model method, do not have to write the dictionary to the model method. Mj_objectwithkeyvalues:dictPanax Notoginseng //Nsarray *dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Food_types.plis T "Oftype:nil]"; - //Nsmutablearray *arraym = [Nsmutablearray array]; the //For (nsdictionary *dict in Dictarray) { + //ysfoodtypemodule *foodt = [Ysfoodtypemodule mj_objectwithkeyvalues:dict]; A //[Arraym Addobject:foodt]; the //        } + //_foodtypes = Arraym; -          $         //----2, using the dictionary array in mjextension to model array method, do not have to write the dictionary to the model method. Mj_objectarraywithkeyvaluesarray:dictarray $ //Nsarray *dictarray = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "Food_types.plis T "Oftype:nil]"; - //_foodtypes = [Ysfoodtypemodule Mj_objectarraywithkeyvaluesarray:dictarray]; -          the         //----3, using the dictionary array in mjextension to model array method, do not have to write the dictionary to the model method. Mj_objectarraywithfile:filepath - //nsstring *filepath = [[NSBundle mainbundle] pathforresource:@ "food_types.plist" oftype:nil];Wuyi //_foodtypes = [Ysfoodtypemodule Mj_objectarraywithfile:filepath]; the          -         //----4, using the dictionary array in mjextension to model array method, do not have to write the dictionary to the model method. Mj_objectarraywithfilename Wu_foodtypes = [Ysfoodtypemodule mj_objectarraywithfilename:@"food_types.plist"]; -          About     } $     return_foodtypes; - } -  -- (void) Viewdidload { A [Super Viewdidload]; +  the}

Three methods of dictionary to model four: summary

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.