標籤:技術分享 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 字典轉模型