//  appModel.h
// Created by zzqqrr on 17/8/19.
//
#import <Foundation/Foundation.h> @interface appModel : NSObject
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *icon;
- (id)initWithDict:(NSDictionary *)dict;
/**类方法用+号*/
+ (id)appWithDict:(NSDictionary *)dict;
@end //
// appModel.m
// Created by zzqqrr on 17/8/19.
//
#import "appModel.h"

@implementation appModel
//- (id)initWithDict:(NSDictionary *)dict ios7以前不推荐用id
- (instancetype)initWithDict:(NSDictionary *)dict
{
if(self==[super init])
{
self.title=dict[@"title"];
self.icon=dict[@"icon"];
}
return self;
}
//- (id)appWithDict:(NSDictionary *)dict ios7以前不推荐用id
//直接以类名方式调用,不用alloc 规范标准推荐加上
+(instancetype)appWithDict:(NSDictionary *)dict
{
//return [[appModel alloc] initWithDict:dict];
return [[self alloc] initWithDict:dict];
}
@end

//懒加载应用数据
- (NSArray *)apps
{
if(_apps==nil)
{
//获得plist文件全路径
NSString *path=[[NSBundle mainBundle] pathForResource:@"main.plist" ofType:nil];
//加载数组
//_apps=[NSArray arrayWithContentsOfFile:path];
NSArray *dictArray=[NSArray arrayWithContentsOfFile:path];
NSMutableArray *appArray=[NSMutableArray array];
//把字典数组转换成模型对象,放到数组中
for (NSDictionary *dict in dictArray) {
appModel *apps=[[appModel alloc] initWithDict:dict];
appModel *test=[appModel appWithDict:dict];//类名方式调用,同上//把对象添加到数组上
[appArray addObject:apps];
}
_apps=appArray;
}
return _apps;
}
@interface ViewController ()
@property (nonatomic,strong) NSArray *apps;
@end NSDictionary *dict=self.apps[index];//字典
appModel *model=self.apps[index];//模型

ios中字典转模型的创建以及简单用法的更多相关文章

  1. IOS中TableView的使用(1) -创建一个简单的tableView

    创建一个简单的tableView: #import <UIKit/UIKit.h> /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITabl ...

  2. iOS开发—字典转模型,KVC设计模式

    iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...

  3. iOS-字典转模型(单模型)的实现

    用模型取代字典的好处 使用字典的坏处 一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编译器不会有任何友善提示,需要手敲, eg:dict[@"name&quo ...

  4. python中字典dic详解-创建,遍历和排序

    原文地址:http://www.bugingcode.com/blog/python_dic_create_sort.html 在python的编程中,字典dic是最典型的数据结构,看看如下对字典的操 ...

  5. iOS中数据库运用之前的准备-简单的数据库

    1.数据持久化 数据持久化是通过文件将数据存储在硬盘上 iOS下主要有四种数据持久化方式 属性列表 对象归档 SQLite数据库 CoreData 数据持久化对的对比 1.属性列表.对象归档适合小数据 ...

  6. Java中Comparable接口和Comparator接口的简单用法

    对象比较器 1.Comparable接口 此接口强行对实现它的每个类的对象进行整体排序,这种排序成为类的自然排序,类的compareTo方法称为类的自然比较方法. 代码示例 import java.u ...

  7. python3中time模块与datetime模块的简单用法

    __author__ = "JentZhang" import time # Timestamp 时间戳 print("Timestamp 时间戳:") pri ...

  8. iOS开发——笔记篇&关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结

    关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结 一:Plist读取 /************************************ ...

  9. iOS中多线程原理与runloop介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...

随机推荐

  1. hdu5992 kdt

    题意:n个旅馆,每个有花费,m个查询,查询在某个点在c花费范围内的距离最小的旅馆 题解:kdt,建成四维,坐标两维,花费一维,id一维,实际上建树只用前两维,正常的查询,如果满足条件在更新答案即可 / ...

  2. leetcode-algorithms-35 Search Insert Position

    leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...

  3. poj2117-tarjin求割点

    http://poj.org/problem?id=2117 求移除一个点以及与它相邻边后,剩下的图中最大的联通子图的数量是多少. 跑一遍tarjin统计下拆除某个点剩下的子图数量即可.注意给出的图不 ...

  4. 十八、Spring框架(AOP)

    一.AOP(基于XML方式配置AOP) AOP(Aspect Oriented Program):面向切面编程.思想是:把功能分为核心业务功能和周边功能. 所谓核心业务功能:比如登录,增删改数据都叫做 ...

  5. Redis php常用操作

    $redis = new redis(); //连接 $redis->connect('127.0.0.1',6379); // //设置值 $result = $redis->set(' ...

  6. 基于spring的PropertySource类实现配置的动态替换

    public class ConfigPropertySource extends PropertySource<Properties> implements PriorityOrdere ...

  7. Qt画笔实现折线图

    参考:https://www.cnblogs.com/lsgxeva/p/7821550.html效果图: void BrokenLine::paintEvent(QPaintEvent *event ...

  8. python格式化日期

    #!/usr/bin/python # -*- coding: UTF-8 -*- import time import calendar """ 时间元组(年.月.日. ...

  9. POJ 1001 Exponentiation(大数运算)

    POJ 1001 Exponentiation 时限:500 ms   内存限制:10000 K 提交材料共计: 179923   接受: 43369 描述:求得数R( 0.0 < R < ...

  10. Win10系列:UWP界面布局进阶3

    与以往的Windows操作系统不同,Windows 10操作系统在正式版当中取消了任务栏中的"开始"按钮,将大部分的应用程序图标放置在开始屏中,同时将系统设置等常用功能整合到了Ch ...