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