ObjectMapper - 初始化对象 IOS
ObjectMapper - initialize object IOS
让我头疼的简单事情 - 如何初始化符合可映射协议的对象,还没有任何 JSON。
我想做的就是在代码中初始化空的用户对象,如下所示:
let user = User()
然而这给了我错误:
"missing argument for parameter #1 in call"
我可以在 swift 1.2 的 0.14 版本中做到这一点,但现在它不起作用了。你们现在知道如何在 swift 2 和新的 Object Mapper 中做到这一点吗? (我知道如何用 json 等初始化它,我只是想为其他目的初始化该对象,但我不知道如何操作)
class User: Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: NSDate?
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
username <- map["username"]
age <- map["age"]
weight <- map["weight"]
array <- map["arr"]
dictionary <- map["dict"]
bestFriend <- map["best_friend"]
friends <- map["friends"]
birthday <- (map["birthday"], DateTransform())
}
}
请帮忙!
以下应该有效:
class User: NSObject, Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: NSDate?
override init() {
super.init()
}
convenience required init?(_ map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
username <- map["username"]
age <- map["age"]
weight <- map["weight"]
array <- map["arr"]
dictionary <- map["dict"]
bestFriend <- map["best_friend"]
friends <- map["friends"]
birthday <- (map["birthday"], DateTransform())
}
}
以上答案的固定版本:
init() {}
required convenience init?(_ map: Map) { self.init() }
让我头疼的简单事情 - 如何初始化符合可映射协议的对象,还没有任何 JSON。
我想做的就是在代码中初始化空的用户对象,如下所示:
let user = User()
然而这给了我错误: "missing argument for parameter #1 in call"
我可以在 swift 1.2 的 0.14 版本中做到这一点,但现在它不起作用了。你们现在知道如何在 swift 2 和新的 Object Mapper 中做到这一点吗? (我知道如何用 json 等初始化它,我只是想为其他目的初始化该对象,但我不知道如何操作)
class User: Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: NSDate?
required init?(_ map: Map) {
}
// Mappable
func mapping(map: Map) {
username <- map["username"]
age <- map["age"]
weight <- map["weight"]
array <- map["arr"]
dictionary <- map["dict"]
bestFriend <- map["best_friend"]
friends <- map["friends"]
birthday <- (map["birthday"], DateTransform())
}
}
请帮忙!
以下应该有效:
class User: NSObject, Mappable {
var username: String?
var age: Int?
var weight: Double!
var array: [AnyObject]?
var dictionary: [String : AnyObject] = [:]
var bestFriend: User? // Nested User object
var friends: [User]? // Array of Users
var birthday: NSDate?
override init() {
super.init()
}
convenience required init?(_ map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
username <- map["username"]
age <- map["age"]
weight <- map["weight"]
array <- map["arr"]
dictionary <- map["dict"]
bestFriend <- map["best_friend"]
friends <- map["friends"]
birthday <- (map["birthday"], DateTransform())
}
}
以上答案的固定版本:
init() {}
required convenience init?(_ map: Map) { self.init() }