Skip to content

👔A super fast & convenient object mapper tailored for your needs

License

Notifications You must be signed in to change notification settings

zenangst/Tailor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 Cannot retrieve latest commit at this time.

History

85 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tailor Swift logo

A super fast & convenient object mapper tailored for your needs.

Mapping objects to arrays or dictionaries can be a really cumbersome task, but those days are over. Tailor features a whole bunch of nifty methods for your model sewing needs.

Mapping properties

Tailor features property, object(s) mapping for both struct and class objects.

Struct

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  init(_ map: JSONDictionary) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Class

class Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""

  required convenience init(_ map: [String : AnyObject]) {
    self.init()
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
  }
}

let dictionary = ["first_name" : "Taylor", "last_name" : "Swift"]
let model = Person(dictionary)

Object mapping

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?

  init(_ map: JSONDictionary) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.object("spouse")
  }
}

let dictionary = [
  "first_name" : "Taylor", 
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin", 
              "last_name" : "Harris"]
]
let model = Person(dictionary)

Mapping objects

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()

  init(_ map: JSONDictionary) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.object("spouse")
    parents   <- map.objects("parents")
  }
}

let dictionary = [
  "first_name" : "Taylor", 
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin", 
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea", 
              "last_name" : "Swift"],
              ["first_name" : "Scott", 
              "last_name" : "Swift"]
  ]
]
let model = Person(dictionary)

Transforms

struct Person: Mappable {

  var firstName: String? = ""
  var lastName: String? = ""
  var spouse: Person?
  var parents = [Person]()
  var birthDate = NSDate?

  init(_ map: JSONDictionary) {
    firstName <- map.property("first_name")
    lastName  <- map.property("last_name")
    spouse    <- map.object("spouse")
    parents   <- map.objects("parents")
    birthDate <- map.transform("birth_date", transformer: { (value: String?) -> NSDate? in
      guard let value = value else { return nil }
      let dateFormatter = NSDateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd"
      return dateFormatter.dateFromString(value)
    })
  }
}

let dictionary = [
  "first_name" : "Taylor", 
  "last_name" : "Swift",
  "spouse" : ["first_name" : "Calvin", 
              "last_name" : "Harris"],
  "parents" : [
             ["first_name" : "Andrea", 
              "last_name" : "Swift"],
              ["first_name" : "Scott", 
              "last_name" : "Swift"]
  ],
  "birth_date": "1989-12-13"
]
let model = Person(dictionary)

Installation

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

pod 'Tailor'

Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create pull request

Who made this?

About

👔A super fast & convenient object mapper tailored for your needs

Resources

License

Stars

Watchers

Forks

Packages

No packages published