IOS 字典转模型

Posted 守望星空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IOS 字典转模型相关的知识,希望对你有一定的参考价值。

一.普通的字典模型:(实例)

Aps.h

//模型类:用来存放数据的类


#import <Foundation/Foundation.h>
/**
 copy:NSString
 strong:一般对角
 weak:UI控件
 assign:基本数据类型
 
 */

@interface Apps : NSObject

/**名称*/
@property (nonatomic,copy)NSString *name;

/**图标*/
@property(nonatomic,copy)NSString *icon;

/**
 通过字典来初始化模型对象
 @param dict 字典对象
 @return 已经初始化完毕的模型对象s
 */
-(instancetype)initWithDict:(NSDictionary *) dict;
+(instancetype)appWithDict:(NSDictionary *)dict;
@end
View Code

Aps.m

#import <Foundation/Foundation.h>

#import "Apps.h"

@implementation Apps

-(instancetype)initWithDict:(id) dict
{
if(self=[super init])
{

    self.name=dict[@"name"];
    self.icon=dict[@"icon"];
}
    return self;
}


+(instancetype)appWithDict:(NSDictionary *)dict
{
//    MjAPP *app=[[MjAPP alloc]initWithDict:dict];
//    return app;

    return [[self alloc]initWithDict:dict];
}

@end
View Code

字典对象转模型对象: 

/**获取plist文件的数组数据*/
-(NSArray *)aps{
    
    if(_aps==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创建模型对象
            Apps *app=[Apps appWithDict:dict];
            
          //3.2将字典的所有属性赋值给模型
//            app.name=dict[@"name"];
//            app.icon=dict[@"icon"];
            

            
            //3.3添加模型对象到数组中
            [appArray addObject:app];
        }
        //4.赋值
        _aps=appArray;
    }
    
    return _aps;
}
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

 

 

二。字典转模型中的模型(实例)

MJCars.h

//
//  MJCars.h
//  imgCarBrand
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MJCars : NSObject


@property(nonatomic,strong) NSArray *icon;
@property (nonatomic ,copy)NSString *name;

+(instancetype)carWithDict:(NSDictionary *)dict;

-(instancetype)initWithDict:(NSDictionary *)dict;
@end
View Code

 

MJCars.m

//
//  MJCars.m
//  imgCarBrand
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "MJCars.h"

@implementation MJCars

+(instancetype)carWithDict:(NSDictionary *)dict{

    return [[self alloc]initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict;{

    if(self=[super init])
    {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end
View Code

 

MJCarGroup.h

//
//  MJCarGroup.h
//  imgCarBrand
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface MJCarGroup : NSObject

@property (nonatomic ,copy)NSString *title;

@property(nonatomic,strong) NSArray *cars;

+(instancetype)carGroupWithDict:(NSDictionary *)dict;

-(instancetype)initWithDict:(NSDictionary *)dict;

@end
View Code

 

MJCarGroup.m

//
//  MJCarGroup.m
//  imgCarBrand
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "MJCarGroup.h"
#import "MJCars.h"

@implementation MJCarGroup
+(instancetype)carGroupWithDict:(NSDictionary *)dict{

    return [[self alloc]initWithDict:dict];
}

-(instancetype)initWithDict:(NSDictionary *)dict{

    if(self=[self init])
    {
        //赋值标题
        self.title=dict[@"title"];
        
       //取出原来 的字典数组
        NSArray *dictArray=dict[@"cars"];
        NSMutableArray *carArray=[NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            MJCars *car=[MJCars carWithDict:dict];
            [carArray addObject:car];
        }
        self.cars=carArray;
    }
    return self;
}
@end
View Code

 

ViewController.m

//
//  ViewController.m
//  imgCarBrand
//
//  Created by apple on 17/2/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ViewController.h"
#import "MJCarGroup.h"
#import "MJCars.h"

@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

//车品牌组数据
@property(nonatomic,strong) NSArray *groups;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   }


-(NSArray *)groups
{
    if(_groups==nil)
    {
    
        //初始化
        //1.获得plist的全路径
        NSString *path=[[NSBundle mainBundle]pathForResource:@"cars_total.plist" ofType:nil];
        //2.加载数组
        NSArray *dictArray=[NSArray arrayWithContentsOfFile:path];
        
        //3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *groupArray=[NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            //3.1创建模型对象
            MJCarGroup *group=[MJCarGroup carGroupWithDict:dict];
            
            //3.2添加模型对象到数组中
            [groupArray addObject:group];
        }
        //赋值
        _groups=groupArray;
    }
    return _groups;
}

#pragma mark -数据源方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MJCarGroup *group=self.groups[section];
    return group.cars.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //1.定义一个循环标识
    static NSString *ID=@"car";
    
    //2.从缓存池中取出可循环利用cell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    
    //3.缓存池中没有可循环得用的cell
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    //4.设置数据
    MJCarGroup *group=self.groups[indexPath.section];
    MJCars *car=group.cars[indexPath.row];
    
    cell.imageView.image=[UIImage imageNamed:car.icon];
    cell.textLabel.text=car.name;
    
    return cell;
    
}

/**第section组显示的头部标题*/
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    MJCarGroup *group=self.groups[section];
    return group.title;
}


/**返回右边索引条显示的字符串数据*/
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [self.groups valueForKeyPath:@"title"];
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  }


@end
View Code

 

以上是关于IOS 字典转模型的主要内容,如果未能解决你的问题,请参考以下文章

ios学习8_KVC和字典转模型

IOS - - 字典转模型

IOS 字典转模型

iOS应用管理(字典转模型)

ios开发runtime学习五:KVC以及KVO,利用runtime实现字典转模型

字典转模型框架 Mantle的使用:国外程序员最常用的iOS模型