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] AlamofireModel

class 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: args
        if key == "args" {
            
            let dictionary = value as! [String : AnyObject]
            let model      = ArgsModel(dictionary: dictionary)
            
            super.setValue(model, forKey: key)
            return
        }

        // Dictionary: headers
        if 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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值