ios中字典转模型的创建以及简单用法
// 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中字典转模型的创建以及简单用法的更多相关文章
- IOS中TableView的使用(1) -创建一个简单的tableView
创建一个简单的tableView: #import <UIKit/UIKit.h> /*tableView 一定要遵守这两个协议: UITableViewDataSource,UITabl ...
- iOS开发—字典转模型,KVC设计模式
iOS开发UI基础—字典转模型 开发中,通常使用第三方框架可以很快的实现通过字典转模型,通过plist创建模型,将字典的键值对转成模型属性,将模型转成字典,通过模型数组来创建一个字典数组,通过字典数组 ...
- iOS-字典转模型(单模型)的实现
用模型取代字典的好处 使用字典的坏处 一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编译器不会有任何友善提示,需要手敲, eg:dict[@"name&quo ...
- python中字典dic详解-创建,遍历和排序
原文地址:http://www.bugingcode.com/blog/python_dic_create_sort.html 在python的编程中,字典dic是最典型的数据结构,看看如下对字典的操 ...
- iOS中数据库运用之前的准备-简单的数据库
1.数据持久化 数据持久化是通过文件将数据存储在硬盘上 iOS下主要有四种数据持久化方式 属性列表 对象归档 SQLite数据库 CoreData 数据持久化对的对比 1.属性列表.对象归档适合小数据 ...
- Java中Comparable接口和Comparator接口的简单用法
对象比较器 1.Comparable接口 此接口强行对实现它的每个类的对象进行整体排序,这种排序成为类的自然排序,类的compareTo方法称为类的自然比较方法. 代码示例 import java.u ...
- python3中time模块与datetime模块的简单用法
__author__ = "JentZhang" import time # Timestamp 时间戳 print("Timestamp 时间戳:") pri ...
- iOS开发——笔记篇&关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结
关于字典plist读取/字典转模型/自定义View/MVC/Xib的使用/MJExtension使用总结 一:Plist读取 /************************************ ...
- iOS中多线程原理与runloop介绍
一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...
随机推荐
- wordpress +window 走起~
一.安装XAMPP 1 百度搜索xampp后下载安装到D盘(安装时按默认勾选安装即可) 2 安装完后,点击Start来启动Apache和MySQL这两个服务 3 如果Apache服务不能启动,多数 ...
- Gifts by the List CodeForces - 681D (思维)
大意: 给定森林, 要求构造一个表, 满足对于每个$x$, 表中第一次出现的$x$的祖先(包括$x$)是$a_x$. 刚开始还想着直接暴力分块优化一下连边, 最后按拓扑序输出... 实际上可以发现$a ...
- Remove Element leetcode java
问题描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- Oracle date timestamp 毫秒 - 时间函数总结(转)
原文地址:Oracle date timestamp 毫秒 - 时间函数总结 yyyy-mm-dd hh24:mi:ss.ff 年-月-日 时:分:秒.毫秒 --上一月,上一年select add_ ...
- Legal or Not (判断是否存在环)
Legal or Not Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- ORM框架之SQLALchemy
一.面向对象应用场景: 1.函数有共同参数,解决参数不断重用: 2.模板(约束同一类事物的,属性和行为) 3.函数编程和面向对象区别: 面向对象:数据和逻辑组合在一起:函数编程:数据和逻辑不能组合在一 ...
- PE文件结构解析
说明:本文件中各种文件头格式截图基本都来自看雪的<加密与解密>:本文相当<加密与解密>的阅读笔记. 1.PE文件总体结构 PE文件框架结构,就是exe文件的排版结构.也就是说我 ...
- Pinpoint是一个开源的 APM (Application Performance Management/应用性能管理)工具,用于基于java的大规模分布式系统,基于Google Dapper论文
Pinpoint是一个开源的 APM (Application Performance Management/应用性能管理)工具,用于基于java的大规模分布式系统,基于Google Dapper论文 ...
- anglar cli的 rxjs_1.of is not a function
按照官网敲例子遇到 rxjs_1.of is not a function import { Observable,of } from 'rxjs'; 改为 import { Observable} ...
- Ubuntu云服务器下mysql授权远程登陆
1)首先以 root 帐户登陆 MySQL(在授权之前要确保3306端口开放)2)创建远程登陆用户并授权 > grant all PRIVILEGES on discuz.* to zhan@' ...