Swift - 从字典(或者Alamofire)直接创建Model文件的工具
Swift - 从字典(或者Alamofire)直接创建Model文件的工具
效果
1. 常规生成model的方式
2. 通过debug创建model的方式
特性
1. 可以处理JSON格式的字典数据
2. 可以处理本地的json数据
3. 可以处理Alamofire生成的json格式返回数据
4. 生成的Models继承自NSObject,所有方法均系系统方法,没有任何接口污染,后续升级不存在版本兼容问题(以下是一个生成的Model的示例)
// // AlamofireModel.swift // // http://www.cnblogs.com/YouXianMing/ // https://github.com/YouXianMing // // Copyright (c) YouXianMing All rights reserved. // import Foundation// MARK: [Class] AlamofireModelclass AlamofireModel: NSObject {// MARK: Stored propeties.//----------------------------------------------------------------------------- var origin : String?var url : String?var args : ArgsModel?var headers : HeadersModel?// MARK: Init methods.//-----------------------------------------------------------------------------/**Init with dictionary.- parameter dictionary: The json data dictionary.- returns: The instance.*/init?(dictionary : [String : AnyObject]?) {super.init()if let _ : [String : AnyObject] = dictionary { setValuesForKeysWithDictionary(dictionary!) } else { return nil}}/**Override init.- returns: The instance.*/override init() {super.init()}// MARK: SetValueForKey & setValueForUndefinedKey.//-----------------------------------------------------------------------------/**Sets the property of the receiver specified by a given key to a given value.- parameter value: The value for the property identified by key.- parameter key: The name of one of the receiver's properties.*/override func setValue(value: AnyObject?, forKey key: String) {// To ignore Null value.guard value != nil else {return}// Dictionary: argsif key == "args" {let dictionary = value as! [String : AnyObject]let model = ArgsModel(dictionary: dictionary)super.setValue(model, forKey: key)return}// Dictionary: headersif key == "headers" {let dictionary = value as! [String : AnyObject]let model = HeadersModel(dictionary: dictionary)super.setValue(model, forKey: key)return}super.setValue(value, forKey: key)}/**Invoked by setValue:forKey: when it finds no property for a given key.- parameter value: The value for the key identified by key.- parameter key: A string that is not equal to the name of any of the receiver's properties.*/override func setValue(value: AnyObject?, forUndefinedKey key: String) {// [Example] change property 'id' to 'userId'.//// if key == "id" {//// userId = value as? NSNumber// return// } print("[‼️] The file '\(self.classForCoder).swift' has an undefined key '\(key)', and the key's type is \(value?.classForCoder).")} }
源码
https://github.com/YouXianMing/Create-Swift-JSON-Model/tree/master
转载于:https://www.cnblogs.com/YouXianMing/p/5814352.html
Swift - 从字典(或者Alamofire)直接创建Model文件的工具相关推荐
- 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版
技术背景 KML,是标记语言(Keyhole Markup Language)的缩写,最初由Keyhole公司开发,是一种基于XML 语法与格式的.用于描述和保存地理信息(如点.线.图像.多边形和模型 ...
- SNMP功能开发简介 五 使用MIB Builder创建MIB文件图文介绍
开发snmp功能一般都会涉及到添加私有节点,此外还要提供给其他人私有节点的MIB文件以方便查看.这里使用MG SOFT提供的MIB Builder工具创建MIB文件,工具下载链接:https://pa ...
- Windows 系统部署之创建应答文件
1.从微软官网下载创建应答文件的工具(Windows Assessment and Deployment Kit,ADK)并安装,安装方法与普通软件没什么区别,在如图过程时,全部勾选安装: 2.安装完 ...
- swift之字典转模型kvc、mjextention桥接、反射、HandyJSON、ObjectMapper、Codable
参考swift4.0字典转模型:https://www.cnblogs.com/shaoting/p/8087153.html =====================kvc字典转模型======= ...
- swift 的网络解析 Alamofire
我们都知道AFNetworking在国内多牛X,这哥们又写了swift的第三方请求 Alamofire 这到底怎么使用呢 我们可以通过GitHub上下载东西 当然我们也可以使用 cocoaPods 1 ...
- sqlalchemy根据表名动态创建model类
sqlalchemy根据表名动态创建model类 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/J_Object/article/details/8 ...
- Swift - 添加纯净的Alamofire
Swift - 添加纯净的Alamofire 如果你有代码洁癖,不能容忍任何多余的东西,请继续往下看. 1. 下载Alamofire (https://github.com/Alamofire/Al ...
- python批量创建实例_Django批量创建Model实例
1.前言: 将测试数据全部敲入数据库非常繁琐,而且如果与合作伙伴一起开发,部署,那么他们肯定也不想把时间花在一个一个录入数据的繁琐过程中,这时候,创建一个批量录入数据的脚本(population sc ...
- Swift游戏实战-跑酷熊猫 01 创建工程导入素材
原文:Swift游戏实战-跑酷熊猫 01 创建工程导入素材 在这节里,我们将建立一个游戏工程,并导入一些必要的素材,例如序列帧动画文件,声音素材文件.动画文件我们使用atlas形式.在打包发布或者模拟 ...
最新文章
- 【转载】写博意味着什么?
- 从陈坤微信号说起:微信公众平台开发者的江湖
- FireFox不支持cursor:hand
- MySQL二进制日志文件的用法_数据恢复
- 阿里开发者们的第16个感悟:让阅读源码成为习惯
- linux cpu频率软件,linux cpu频率控制
- linux dhcp服务软包,dpkg包管理器详解
- hadoop--集群时间同步(可不同步)
- rust笔记10 泛型处理
- python设置二维列表_Python设置为列表
- SilkTest 对 Excel 表操作的两种方式
- 神经网络基础模型--Logistic Regression的理论和实践
- 一款基于SpringBoot + Spring Security的后台管理系统,强烈推荐,直接用
- 如何理解海森堡的「不确定性原理」?
- java学习笔记(8) 第8章(下)- 面向对象编程(中级部分) - 练习题
- 【云原生之Docker实战】使用docker部署DokuWiki知识库系统
- 2021-10-16windows系统还原点创建/查看/配置/删除
- 爱江山更爱美人服务器维修怎么,爱江山更爱美人落星院详细玩法攻略
- 6 生僻字_《生僻字》歌词拼音与注释,跟着音乐学汉字
- QT 如何去除QListView选中item后的虚线框
热门文章
- mysql 建表代码
- Netcraft :2019年4月全球Web服务器排名 nginx首度登顶
- python人脸识别环境搭建_怎样用3分钟搭建 Python 人脸识别系统
- 理解Session实现原理及安全运用
- C语言3中方法判断32还是64位机
- Java中的日期操作
- spring boot 报错:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default p
- Luogu2495[SDOI2011]消耗战
- jQuery练习:表单模态框
- 设计模式 | 适配器模式及典型应用