美文网首页iOS自学之路
字典转模型详解

字典转模型详解

作者: 木_木27 | 来源:发表于2015-06-20 12:03 被阅读1575次

第一级别

加载plist文件,直接面对字典开发

  • 设置plist文件(死数据):


    plist文件
  • 加载plist以及面对字典开发:

//加载plist文件
NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
//这是个装着字典的数组
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
//搞个属性来保存字典数组
@property (strong, nonatomic) NSArray *shops;
//赋值
self.shops = dictArray;
//根据字典数据来设置控件的数据
NSDictionary *data = self.shops[index];
pic.image = [UIImage imageNamed:data[@"icon"]];//pic为UIImageView
word.text = data[@"name"];//word为UILabel

第二级别

加载plist文件,直接面对模型

  • 加载plist文件;
  • 新建模型类,创建模型对象:


    模型类
  • 如果plist文件里面字典有几个key,模型类就应该声明几个属性
/**图片*/
@property (strong, nonatomic) NSString *icon;
/**名字*/
@property (strong, nonatomic) NSString *name;
  • 在控制器文件里面进行字典转模型
//创建数组
self.shops = [NSMutableArray array];
//加载plist文件
NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
/**************  字典转模型  **************/
for (NSDictionary *dict in dictArray) {//遍历字典数组
//创建模型对象
XMGShop *shop = [[XMGShop alloc] init];
//字典转模型
    shop.icon = dict[@"icon"];
    shop.name = dict[@"name"];
    //将模型存放到数组当中
    [self.shops addObject:shop];
}

//根据模型数据来设置控件的数据

//将模型数组里面的模型对象取出来
XMGShop *shop = self.shops[index];
pic.image = [UIImage imageNamed:shop.icon];//pic为UIImageView
word.text = shop.name;//word为UILabel

第二级别(封装)

//创建数组
self.shops = [NSMutableArray array];

//加载plist文件
NSString *file = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:file];
/**************  字典转模型  **************/
for (NSDictionary *dict in dictArray) {//遍历字典数组
    //创建模型对象并转为模型
//            XMGShop *shop = [[XMGShop alloc] initWithDict:dict];
    XMGShop *shop = [XMGShop shopWithDict:dict];
    //将模型存放到数组当中
    [self.shops addObject:shop];
}
  • 模型类内部封装好的方法:
-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {//一定要调回父类的方法
        //字典转模型
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
    }
    return self;
}
+(instancetype)shopWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];//这里一定要用self
}

图解

字典转模型

相关文章

  • Object-C字典转模型

    参考资料: 字典转模型详解 字典转模型简介 把字典转成一个类,我们把这个类叫着模型。模型是对字典的一种抽象和封装,...

  • 字典转模型详解

    第一级别 加载plist文件,直接面对字典开发 设置plist文件(死数据):plist文件 加载plist以及面...

  • 14-Swift中字典转模型

    字典转模型(初始化时传入字典) 字典转模型(利用KVC转化) 一、 普通的字典转模型: 二、利用KVC字典转模型:

  • Swift 5.0 使用MJExtension 字典转模型

    记录下swift怎么使用MJExtension 字典转模型1.普通的字典转模型 2.字典数组嵌套转模型

  • model模型无法直接转model模型

    model模型无法直接转model模型 可以model转字典,字典再转model: NSDictionary *v...

  • 字典转模型,模型转字典

    //字典转模型 - (id)initWithDictionary:(NSDictionary*)dic{ self...

  • iOS-模型

    在开发中,经常使用到模型,通常做法就是字典转模型 字典转模型的过程最好封装在模型内部 模型应该提供一个可以传入字典...

  • Swift 字典转模型

    这里探讨字典转模型中模型的类型单一模型模型嵌套 (模型中包含模型 || 模型中包含模型数组) Swfit的字典转模...

  • iOS 字典转模型 runtime实现

    写在前面的话 这篇文章的通过runtime实现字典转模型是参考(抄袭)iOS 模式详解—「runtime面试、工作...

  • YYModel-字典转模型篇章

    字典转模型 字典转模型通过两个入口函数_yy_dictionaryWithJSON和yy_modelWithDic...

网友评论

  • rectinajh:请问,字典转模型有哪些好处?可以应用到哪下地方?
    字典转模型跟数据库存储有什么关系?
  • 飞舞的指尖:小码哥大神

本文标题:字典转模型详解

本文链接:https://www.haomeiwen.com/subject/frvsqttx.html