I have an init method to init my model with JSON data, however when I want to also store this object in NSUserDefaults ill need to create acoder and adecoder init methods.
Im getting the error:
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)'
the init for aDecoder is:
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)
}
but I already have this init:
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
}
How do reconcile both to work without throwing this error?