Swift and swiftcode

Source: Internet
Author: User

Swift and swiftcode

Swift-A Tool for directly creating Model files from the dictionary (or Alamofire)

 

Effect

1. General model generation method

2. Create a model through debug

 

 

Features

1. it can process Dictionary data in JSON format

 

2. Processing local json data

 

3. data can be returned in json format generated by Alamofire.

 

4. The generated Models inherits from NSObject. All the methods are system methods without any interface pollution. Subsequent upgrades do not have version compatibility issues (The following is an example of a generated 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).")    }}

 

Source code

Https://github.com/YouXianMing/Create-Swift-JSON-Model/tree/master

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.