Giter Club home page Giter Club logo

modelswift's Introduction

ModelSwift

CI Status Version License Platform Carthage compatible

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

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)
}

ignored property

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

}

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+

Installation

CocoaPods

ModelSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ModelSwift"

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

To integrate ModelSwift into your Xcode project using Carthage, specify it in your Cartfile:

github "hujewelz/modelSwift"

Author

hujewelz

License

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

modelswift's People

Contributors

hujewelz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.