//  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. Numpy常用API

    目录 一.输入和输出 1.1 NumPy二进制文件(NPY,NPZ) 1.2 文本文件 1.3 正则表达式解析 1.4 原始二进制文件 1.5 内存映射文件 1.6 Base-n相关 1.7 数据源 ...

  2. poj2891 扩展中国剩余定理

    求a1x1+r1=y...anxn+rn=y,crt合并 //#pragma GCC optimize(2) //#pragma GCC optimize(3) //#pragma GCC optim ...

  3. 3月22 关于CSS

    CSS(Cascading Style Sheep 叠层样式表,作用是美化HTML网页)/*注释内容*/ 为注释的方法. 样式表的分类: 1.内联样式表 和HTML联合显示,控制精确,但是可重用性差, ...

  4. csu oj 1344: Special Judge

    Description Given a positive integer n, find two non-negative integers a, b such that a2 + b2 = n. I ...

  5. php开启redis

    看下自己phpinfo的信息 php 5.5以下的都有这些文件 到这个地方下载所需要的文件:https://github.com/nicolasff/phpredis/downloads 下载解压   ...

  6. Leetcode 1020. 将数组分成和相等的三个部分

    1020. 将数组分成和相等的三个部分  显示英文描述 我的提交返回竞赛   用户通过次数321 用户尝试次数401 通过次数324 提交次数883 题目难度Easy 给定一个整数数组 A,只有我们可 ...

  7. Object对象的浅拷贝与深拷贝方法详解

    /* ===================== 直接看代码 ===================== */ <!DOCTYPE html> <html> <head& ...

  8. jQuery滚屏插件XSwitch.js

    1.需要有基本的HTML结构 <div style="margin-top: 124px;" id="container" data-XSwitch> ...

  9. 维护一个旧程序 linq2sql,出现row not found or changed的异常

    维护一个旧程序 linq2sql,出现row not found or changed的异常, 查博客园,文章都是一大抄,都不对. 想想之前都能保存的.这个异常是在加了字段之后出现的. 因为用vs.n ...

  10. JavaScript -基础- 变量、常量

    一.变量 <script> var a=1 var b=3 var a= 1;   //使用var 定义变量,分号结尾(可不加,换行符也可) var b=3; var a= 1; var ...