测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最新版本 | 2017年8月 |
SwiftSwift版本 | 3.0 |
SPM支持SPM | ✗ |
由Christoffer Winterkvist,Vadym Markov维护。
一个超级快且方便的面向您的需求的对象映射器。
将对象映射到数组或字典可以是一个非常繁琐的任务,但这些日子已经过去了。Tailor为您的模型缝合需求提供了一系列实用方法。
Tailor为struct
和class
对象提供属性和关系映射。
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通过CocoaPods提供。要安装它,只需将以下行添加到您的Podfile中
pod 'Tailor'
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)