IOS Dictionary goto Model

Source: Internet
Author: User

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

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.