ModelSwift 0.2.1

ModelSwift 0.2.1

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by hujewelz.



  • By
  • hujewelz

ModelSwift

ModelSwift can convert josn (or Data) to model in Swift.

Usage

⚠️ In order to convert json(or Data) to model, the model class must be a subclass of NSObject.

⚠️ If the stored property is Int, Float, Double .etc, it should not be optional.

example:

class User: NSObject {
    var name: String?
    var age = 0 // not var age: Int?
    var desc: String?
}

class Repos: NSObject {
    var title: String?
    var owner: User?
    var viewers: [User]?
}

You can map a json key to a property name. just like this:

// JSON
{
 "title": "ModelSwift",
 "owner": { "name": "hujewelz", "age": 23, "description": "iOS Developer" },
 "viewers": [
     { "name": "hujewelz", "age": 23, "description": "iOS Developer"},
     { "name": "bob", "age": 24 },
     { "name": "jobs", "age": 54 }
 ]
}

Match model property to different JSON key:

var desc: String?
...

extension User: Replacable {
    var replacedProperty: [String : String] {
        return ["desc": "description" ]
    }
}

Property of object type:

var owner: User? // an object
...

extension Repos: Reflectable {
    var reflectedObject: [String : AnyClass] {
        return ["owner": User.self]
    }
}

Property in array:

var viewers: [User]? // an object array
...

extension Repos: ObjectingArray {
    var objectInArray: [String : AnyClass] {
        return ["viewers": User.self]
    }
}

ignored property

extension User: Ignorable {
/// the store properties can not to be converted.
    var ignoredProperty: [String] {
    return ["name"]
    }

}

If the type of an object in json cannot be matched to the property of the model, it can be coverted too.

eg.

// JSON
{
    "name": "jewelz"
    "age": "24"     // string => Int
    "isNew": "1223" // string => Bool
}

// model

var name: String?
var age = 0         // 24 
var isNew = false  // true

When we got the data from our server, we can use func ~><T: NSObject>(lhs: Any, rhs: T.Type) -> T? or func =><T: NSObject>(lhs: Any, rhs: T.Type) -> [T]? to convert it to model or a model array:

// convert to a model object
if let repos = dict ~> Repos.self {
    print(repos)
}

// convert to a model array
if let users = array => User.self {
    print(users)
}

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • Xcode 8.1+
  • Swift 3.0+

Author

hujewelz

License

ModelSwift is available under the MIT license. See the LICENSE file for more info.