Swift,swiftcode

來源:互聯網
上載者:User

Swift,swiftcode

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: 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.