IOS 字典轉模型

來源:互聯網
上載者:User

標籤:技術分享   start   字典   string   atom   檔案   pre   list   表徵圖   

模型類:

//模型類:用來存放資料的類#import <Foundation/Foundation.h>/** copy:NSString strong:一般對角 weak:UI控制項 assign:基礎資料型別 (Elementary Data Type)  */@interface MjAPP : NSObject/**名稱*/@property (nonatomic,copy)NSString *name;/**表徵圖*/@property(nonatomic,copy)NSString *icon;@end
View Code

字典對象轉模型對象:

/**擷取plist檔案的數組資料*/-(NSArray *)apps{        if(_apps==nil)    {    //初始化    //1.獲得.plist的全路徑        NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];                            //2.載入數組        NSArray *dictArray=[NSArray arrayWithContentsOfFile:path];            //3.將dictArray裡面的所有字典轉成模型對角。放到新的數組中        NSMutableArray *appArray=[NSMutableArray array];        for (NSDictionary *dict in dictArray) {        //3.1建立模型對象            MjApp *app=[[MjApp alloc]init];                      //3.2將字典的所有屬性賦值給模型            app.name=dict[@"name"];            app.icon=dict[@"icon"];                        //使用宏定義欄位//            app.name=dict[appName];//            app.icon=dict[appIcon];                        //3.3添加模型對象到數組中            [appArray addObject:app];        }    }        return _apps;}
View Code

調用模型對象

        //使用MjApp模型        MjApp *appInfo=self.apps[index];         //3.4.1添加圖片        UIImageView *iconView=[[UIImageView alloc]init];        CGFloat iconW=35;        CGFloat iconH=35;        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"]];                //使用模型對象 icon        iconView.image=[UIImage imageNamed:appInfo.icon];                [appView addSubview:iconView];                //3.4.2 添加名字        UILabel *nameLabel=[[UILabel alloc]init];        CGFloat namgeX=0;        CGFloat nameY=iconY+iconH;        CGFloat nameW=appW;        CGFloat nameH=20;        nameLabel.frame=CGRectMake(namgeX, nameY, nameW, nameH);        nameLabel.backgroundColor=[UIColor greenColor];       // nameLabel.text=appInfo[@"name"];        //使用模型對角 name        nameLabel.text=appInfo.name;                        nameLabel.font=[UIFont systemFontOfSize:8];//設定字型大小        nameLabel.textAlignment=NSTextAlignmentCenter;//字型置中        [appView addSubview:nameLabel];
View Code

 

IOS 字典轉模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.