Skip to content

sheepy1/JSONModelMapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

JSONModelMapper

Introduction

动手写个 JSON-Model Mapping 库

Usage

Demo JSON:

{ "array": [ { "birthday": 2015-12-24, "name": "sheepy" }, { "birthday": 2015-12-24, "name": "sleepy" } ], "boolean": true, "number": 123, "string": "Hello World" }

Define Model

class JSONModel: NSObject {
    var array = []
    var boolean = false
    var number = 0
    var string = ""
}

class PersonModel: NSObject {
    var birthday = NSDate()
    var name = ""
}

Send request and convert data to optional model

getDataFromUrl(Constant.DemoAPI, method: .GET, parameter: nil) { data, error in
    if let jsonData = data {
        let jsonModel = jsonData => JSONModel.self
    }
    
    if let httpError = error {
        print(httpError)
    }
}

Convert AnyObject to optional model

if let model = jsonModel {
    let person = model.array[0] => PersonModel.self 
}

Convert model to NSData

let data = person => NSData.self

Convert model to Dictionary

let dict = person.convertToDictinary()

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages