TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Aug 2017 |
SwiftSwift Version | 3.0 |
SPMSupports SPM | ✗ |
Maintained by Christoffer Winterkvist, Vadym Markov.
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.
Tailor features property, relation(s) mapping for both struct
and class
objects.
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 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)
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.relation("spouse")
}
}
let dictionary = [
"first_name" : "Taylor",
"last_name" : "Swift",
"spouse" : ["first_name" : "Calvin",
"last_name" : "Harris"]
]
let model = Person(dictionary)
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.relation("spouse")
parents <- map.relations("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)
struct ImmutablePerson: SafeMappable {
let firstName: String
let lastName: String
let spouse: Person
let parents = [Person]()
init(_ map: JSONDictionary) throws {
firstName = try <-map.property("firstName")
lastName = try <-map.property("lastName")
spouse = try <-map.relationOrThrow("spouse")
parents = try <-map.relationsOrThrow("parents")
}
}
let immutablePerson: ImmutablePerson
do {
immutablePerson = try TestImmutable(["firstName" : "foo" , "lastName" : "bar"])
} catch {
print(error)
}
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.relation("spouse")
parents <- map.relations("parents")
birthDate <- map.transform("birth_date", transformer: { (value: String) -> NSDate? in
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)
Tailor is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Tailor'
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)