Model class:
// Model classes: Classes for storing data #import <Foundation/Foundation.h>/* * copy:nsstring Strong: General Diagonal Weak:ui Control Assign: Basic data type */ @interface Mjapp:nsobject /* * Name */ *name; /* * Icon */ *icon; @end
View Code
Dictionary object to model object:
/** Get array data for plist files*/-(Nsarray *) apps{if(_apps==Nil) { //Initialize//1. Get the full path of the. plistNSString *path=[[nsbundle Mainbundle]pathforresource:@"app.plist"Oftype:nil]; //2. Loading an arrayNsarray *dictarray=[Nsarray Arraywithcontentsoffile:path]; //3. Turn all the dictionaries inside the dictarray into the model diagonal. Put it in a new arrayNsmutablearray *apparray=[Nsmutablearray array]; for(Nsdictionary *dictinchDictarray) { //3.1 Creating a Model objectMjapp *app=[[Mjapp alloc]init]; //3.2 Assigning all properties of a dictionary to a modelapp.name=dict[@"name"]; App.icon=dict[@"icon"]; //defining fields with macros//App.name=dict[appname];//App.icon=dict[appicon]; //3.3 Adding a model object to an array[Apparray Addobject:app]; } } return_apps;}View Code
Invoking Model objects
//using the Mjapp modelMjapp *appinfo=Self.apps[index]; //3.4.1 Add a pictureUiimageview *iconview=[[Uiimageview alloc]init]; CGFloat iconw= *; CGFloat Iconh= *; CGFloat IconX= (APPW-ICONW) *0.5; CGFloat Icony=0; Iconview.frame=CGRectMake (IconX, Icony, Iconw, Iconh); //Iconview.backgroundcolor=[uicolor Graycolor]; //iconview.image=[uiimage imagenamed:appinfo[@ "icon"]; //using Model objects iconIconview.image=[UIImage ImageNamed:appInfo.icon]; [Appview Addsubview:iconview]; //3.4.2 Add a nameUILabel *namelabel=[[UILabel alloc]init]; CGFloat Namgex=0; CGFloat Namey=icony+Iconh; CGFloat Namew=APPW; CGFloat Nameh= -; Namelabel.frame=CGRectMake (Namgex, Namey, Namew, Nameh); Namelabel.backgroundcolor=[Uicolor Greencolor]; //namelabel.text=appinfo[@ "name"]; //using the model diagonal namenamelabel.text=Appinfo.name; Namelabel.font=[uifont systemfontofsize:8];//Set Font sizeNamelabel.textalignment=nstextalignmentcenter;//Font Centered[Appview Addsubview:namelabel];
View Code
IOS Dictionary goto Model