iOS中字典转模型的几种常用方式

   字典转模型主要由以下几种方式:

   第一种:在模型文件中只声明属性,字典转模型的整个过程暴漏在控制器中:

   AppModel的.h文件如下:

#import <Foundation/Foundation.h>

@interface AppModel : NSObject

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

  

 此处只有声明属性,故而.m文件中不需要任何操作

  在控制器中进行字典转模型,为了节省内存,需要用数据时候再加载,故而选用懒加载的方式:

- (NSArray *)apps{
   
    if (_apps == nil) {
        //    1.获取全路径
        NSString *path =  [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
        //     2.读取plist文件
       
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        //     3.字典转模型
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            AppModel *appModel = [[AppModel alloc]init];
            appModel.icon = dict[@"icon"];
            appModel.name = dict[@"name"];
            [tempArray addObject:appModel];
           
           
        }
        _apps = tempArray;
       
    }
    return  _apps;
}

  

  第二种字典转模型封装在模型中,在模型文件中提供一个方法用来字典转模型,只需要在控制器中把数据传入即可。

   在AppModel.h文件中的声明:

  #import <Foundation/Foundation.h>

@interface AppModel : NSObject

@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *name;
//提供一个方法出来用来字典转化模型
- ( instancetype)initWithDict:(NSDictionary *)dict;

+ ( instancetype)appModelWithDict:(NSDictionary *)dict;
@end

  

在AppModel.m文件中对模型文件中字典转模型的方法进行实现:

 #import "AppModel.h"

@implementation AppModel
- ( instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
       
    }
    return self;
   
}

  

//plist文件使用的是一样的字典模型,因而此处可以使用kvc操作
- ( instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
       
        [self setValuesForKeysWithDictionary:dict];
   }
    return self;
   
}
+ ( instancetype)appModelWithDict:(NSDictionary *)dict{
    //    AppModel *appModel = [[AppModel alloc]initWithDict:dict];
    return [[self alloc]initWithDict:dict];
   
}

@end

  

控制器中懒加载调用方法字典转模型:

 - (NSArray *)apps{
   
    if (_apps == nil) {
        //    1.获取全路径
        NSString *path =  [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
        //     2.读取plist文件
       
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        //     3.字典转模型
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
        
            AppModel *appModel = [AppModel appModelWithDict:dict];
            [tempArray addObject:appModel];
           
           
        }
        _apps = tempArray;
       
    }
    return  _apps;
}

  

第三种,把整个字典转模型的过程全部封装在类中,定义类方法就行懒加载转模型。

AppModel.h文件中声明方法属性

#import <Foundation/Foundation.h>

@interface AppModel : NSObject

@property(nonatomic,copy)NSString *icon;
@property(nonatomic,copy)NSString *name;
//提供一个方法出来用来字典转化模型
- (instancetype)initWithDict:(NSDictionary *)dict;


+ (instancetype)appModelWithDict:(NSDictionary *)dict;
+ (NSArray *)apps;
@end

  

AppModel.m文件中实现方法,使用kvc

#import "AppModel.h"

@implementation AppModel
- (instancetype)initWithDict:(NSDictionary *)dict{
    if (self = [super init]) {
         [self setValuesForKeysWithDictionary:dict];
       
    }
    return self;
   
}
+ (instancetype)appModelWithDict:(NSDictionary *)dict{
    //    AppModel *appModel = [[AppModel alloc]initWithDict:dict];
    return [[self alloc]initWithDict:dict];
   
}
+ (NSArray *)apps{
   
    if (_apps == nil) {
        //    1.获取全路径
        NSString *path =  [[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
        //     2.读取plist文件
       
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        //     3.字典转模型
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
           
            AppModel *appModel = [AppModel appModelWithDict:dict];
            [tempArray addObject:appModel];
           
           
        }
        _apps = tempArray;
       
    }
    return  _apps;
}
@end

  

 控制器中进行调用字典转模型的过程:

- (NSArray *)apps
{
    if(_apps == nil)
    {
        _apps = [AppModel apps];
       
    }
    return _apps;
}

  

第四种,前面的三种字典转模型的方法仅仅是封装不同,这种方法是不构建对象方法,直接使用类方法。在类方法中创建模型对象,通过对对象的kvc操作提供字典转模型的方法。

AppMode.h文件中声明属性和类方法

#import <Foundation/Foundation.h>

@interface AppModel : NSObject

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

+ (instancetype)appModelWithDict:(NSDictionary *)dict;
@end

  

AppModel.m文件中实现这个类方法的kvc操作

#import "AppModel.h"

@implementation AppModel

+ (instancetype)appModelWithDict:(NSDictionary *)dict{
    //    AppModel *appModel = [[AppModel alloc]initWithDict:dict];
    AppModel *app = [[AppModel alloc] init];
   
    [app setValuesForKeysWithDictionary:dict];
   
    return app;

  

在控制器中就行字典转模型,此处不适用forin,选择系统提供的 enumerateObjectsUsingBlock对字典进行遍历操作,效率更高

- (NSArray *)apps
{
    // 加载数据
    NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil];
    // 得到字典数据
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
   
    // 定义一个临时数组,保存数据模型
    NSMutableArray *arrayM = [NSMutableArray array];
   
    // 遍历数组,字典转模型
    [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        //
        NSDictionary *dict = obj;
       
        // 得到数据模型
        AppModel *app = [AppModel appModelWithDict:dict];
       
        [arrayM addObject:app];
    }];
   
    return arrayM;
}
}

  

原文地址:https://www.cnblogs.com/gaizuojia/p/4652999.html