2017-08-31 34 views
1

我有一个init方法用JSON数据初始化我的模型,但是当我想在NSUserDefaults中存储这个对象时,需要创建acoder和adecoder init方法。用编码器vs Json处理Object init?

即时得到错误:

Cannot invoke 'UserProfileModel.init' with an argument list of type '(apiKey: String, userID: String, userEmail: String, lastName: String, firstName: String, company: String, userImage: String, jobTitle: String)'

为aDecoder初始化为:

required convenience init(coder aDecoder: NSCoder) { 
    let apiKey = aDecoder.decodeObject(forKey: "apiKey") as! String 
    let userID = aDecoder.decodeObject(forKey: "userID") as! String 
    let userEmail = aDecoder.decodeObject(forKey: "userEmail") as! String 
    let lastName = aDecoder.decodeObject(forKey: "lastName") as! String 
    let firstName = aDecoder.decodeObject(forKey: "firstName") as! String 
    let company = aDecoder.decodeObject(forKey: "company") as! String 
    let userImage = aDecoder.decodeObject(forKey: "userImage") as! String 
    let jobTitle = aDecoder.decodeObject(forKey: "jobTitle") as! String 
    self.init(apiKey: apiKey, userID: userID, userEmail: userEmail, lastName: lastName, firstName: firstName, company: company, userImage: userImage, jobTitle: jobTitle) 
} 

,但我已经有这个初始化:

init?(_ json: JSON) { 
    // Map API Key from top level 
    guard let apiKey = json["apikey"].string 
    else { return nil } 

    // assign user 
    guard let userID = json["user"]["id"].string, 
      let userEmail = json["user"]["email"].string, 
      let lastName = json["user"]["lastname"].string, 
      let firstName = json["user"]["firstname"].string 
    else { return nil } 

    // assign optionals to user 
    let company = json["user"]["company"].string 
    let userImage = json["user"]["image"].string 
    let jobTitle = json["user"]["jobtitle"].string 

    // Assign to model properties 
    self.apiKey = apiKey 
    self.userID = userID 
    self.userEmail = userEmail 
    self.lastName = lastName 
    self.firstName = firstName 
    self.company = company 
    self.userImage = userImage 
    self.jobTitle = jobTitle 
} 

如何兼顾双方无劳动抛出这个错误?

回答

2

使您的解码器功能成为必需的init,而不是便利的init。

required init(coder aDecoder: NSCoder) { 
    self.apiKey = aDecoder.decodeObject(forKey: "apiKey") as! String 
    self.userID = aDecoder.decodeObject(forKey: "userID") as! String 
    self.userEmail = aDecoder.decodeObject(forKey: "userEmail") as! String 
    self.lastName = aDecoder.decodeObject(forKey: "lastName") as! String 
    self.firstName = aDecoder.decodeObject(forKey: "firstName") as! String 
    self.company = aDecoder.decodeObject(forKey: "company") as! String 
    self.userImage = aDecoder.decodeObject(forKey: "userImage") as! String 
    self.jobTitle = aDecoder.decodeObject(forKey: "jobTitle") as! String 
} 

这就是所有:)希望它可以帮助

问题是什么?

在iOS中,所有便利的init应该最终调用指定的初始化程序。在你的情况下,你已经有了一个接受jSON的指定初始化器。现在两种解决方案

一个声明指定的init这需要像你在你的代码的init(coder aDecoder: NSCoder)和最后一行中所使用的所有的值让你init(_ json: JSON)也是一个方便的初始化并最终在这两个init(coder aDecoder: NSCoder)init(_ json: JSON)调用指定的初始化

其他只需使用上面指定的第二种简单方法即可。

编辑:

如果我做它需要的,我怎么做用JSON来初始化?生病也需要这样做?

是的,你需要写required init(coder aDecoder: NSCoder) {因为你确认NSCoding协议和NSCoding协议希望你实现这个初始化。因此它是一个必需的init。

作为您的init(_ json: JSON) {是您的班级的指定初始值设定项。

指定初始值设定项和所需初始值设定项有什么区别?

请阅读:What's the difference between a required initializer and a designated initializer?

因为它清楚地总结

  • 需要initialisers不必指定。
  • 指定的初始化器不一定需要。
+0

如果我使它成为必需,我如何用JSON初始化?生病也需要这样做? – jackdm

+0

@jackdm:请阅读我更新的答案我已经指定了为什么你不需要self.init –

+0

@jackdm:请阅读更新的答案。如果有任何混淆,请让我知道 –