1

I am trying to save a list of a custom class to user defaults and I keep receiving the "unrecognized selector sent to instance" error when saving. I have no issues when getting the array from user defaults.

This is the code I am using to save to user defaults.

let userDefaults = UserDefaults.standard
        userDefaults.set(NSKeyedArchiver.archivedData(withRootObject: visitors), forKey: "signedInUsers")
        userDefaults.synchronize()

visitors is an array of Visitor objects.

Here is my Visitor class.

class Visitor: NSObject {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

    // MARK: NSCoding

    override init() {
        super.init()
    }

    init(name: String, contactEmail: String, company: String, picture: UIImage, signature: UIImage,timeIn: String,timeOut: String,signedOut: Bool,otherQuestionsWithAnswers: [String]) {
        self.name = name
        self.contactEmail = contactEmail
        self.company = company
        self.picture = picture
        self.signature = signature
        self.timeIn = timeIn
        self.timeOut = timeOut
        self.signedOut = signedOut
        self.otherQuestionsWithAnswers = otherQuestionsWithAnswers
    }

    required convenience init?(coder decoder: NSCoder) {
        guard let name = decoder.decodeObject(forKey: "name") as? String,
            let contactEmail = decoder.decodeObject(forKey: "contactEmail") as? String,
            let company = decoder.decodeObject(forKey: "company") as? String,
            let timeOut = decoder.decodeObject(forKey: "timeOut") as? String,
            let timeIn = decoder.decodeObject(forKey: "timeIn") as? String,
            let picture = decoder.decodeObject(forKey: "picture") as? UIImage,
            let signature = decoder.decodeObject(forKey: "signature") as? UIImage,
            let otherQuestionsWithAnswers = decoder.decodeObject(forKey: "otherQuestionsWithAnswers") as? [String],
            let signedOut = decoder.decodeObject(forKey: "signedOut") as? Bool
            else { return nil }

        self.init(
            name: name,
            contactEmail: contactEmail,
            company: company,
            picture: picture,
            signature: signature,
            timeIn: timeIn,
            timeOut: timeOut,
            signedOut: signedOut,
            otherQuestionsWithAnswers: otherQuestionsWithAnswers
        )
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encode(name, forKey: "name")
        coder.encode(contactEmail, forKey: "contactEmail")
        coder.encode(company, forKey: "company")
        coder.encode(picture, forKey: "picture")
        coder.encode(signature, forKey: "signature")
        coder.encode(timeIn, forKey: "timeIn")
        coder.encode(timeOut, forKey: "timeOut")
        coder.encode(signedOut, forKey: "signedOut")
        coder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
    }
}

Thanks help is appreciated.

EDIT:

Changing the class to this fixed it.

import Foundation
import UIKit

// a question that the user has to answer
class Visitor: NSObject, NSCoding {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

    // MARK: NSCoding

    override init() {
        super.init()
    }

    init(name: String, contactEmail: String, company: String, picture: UIImage, signature: UIImage,timeIn: String,timeOut: String,signedOut: Bool,otherQuestionsWithAnswers: [String]) {
        self.name = name
        self.contactEmail = contactEmail
        self.company = company
        self.picture = picture
        self.signature = signature
        self.timeIn = timeIn
        self.timeOut = timeOut
        self.signedOut = signedOut
        self.otherQuestionsWithAnswers = otherQuestionsWithAnswers
    }

    required init(coder decoder: NSCoder) {
        name = decoder.decodeObject(forKey: "name") as! String
        contactEmail = decoder.decodeObject(forKey: "contactEmail") as! String
        company = decoder.decodeObject(forKey: "company") as! String
        timeOut = decoder.decodeObject(forKey: "timeOut") as! String
        timeIn = decoder.decodeObject(forKey: "timeIn") as! String
        if let pictureTest = decoder.decodeObject(forKey: "picture") as? UIImage {
            picture = pictureTest
        } else {
            picture = UIImage()
        }
        if let sigTest = decoder.decodeObject(forKey: "picture") as? UIImage {
            signature = sigTest
        } else {
            signature = UIImage()
        }
        otherQuestionsWithAnswers = decoder.decodeObject(forKey: "otherQuestionsWithAnswers") as! [String]
        signedOut = decoder.decodeBool(forKey: "signedOut")
    }

    public func encode(with aCoder: NSCoder) {
        aCoder.encode(name, forKey: "name")
        aCoder.encode(contactEmail, forKey: "contactEmail")
        aCoder.encode(company, forKey: "company")
        aCoder.encode(picture, forKey: "picture")
        aCoder.encode(signature, forKey: "signature")
        aCoder.encode(timeIn, forKey: "timeIn")
        aCoder.encode(timeOut, forKey: "timeOut")
        aCoder.encode(signedOut, forKey: "signedOut")
        aCoder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
    }
}
Vlad Lund
  • 29
  • 5

2 Answers2

0

The function you are using to encode is incorrect, must be func encode(with aCoder: NSCoder){} instead of func encodeWithCoder(coder: NSCoder) {} so change that and works as intended

public func encode(with aCoder: NSCoder) {
    aCoder.encode(name, forKey: "name")
    aCoder.encode(contactEmail, forKey: "contactEmail")
    aCoder.encode(company, forKey: "company")
    aCoder.encode(picture, forKey: "picture")
    aCoder.encode(signature, forKey: "signature")
    aCoder.encode(timeIn, forKey: "timeIn")
    aCoder.encode(timeOut, forKey: "timeOut")
    aCoder.encode(signedOut, forKey: "signedOut")
    aCoder.encode(otherQuestionsWithAnswers, forKey: "otherQuestionsWithAnswers")
}

Hope this helps

Reinier Melian
  • 20,519
  • 3
  • 38
  • 55
0

Your Visitor class should first inherit from NSObject followed by NSCoding. Without the NSCoding, you cannot encode and decode custom classes. This is a recent mistake which I made in my project, and your encode function signature is incorrect. I'm sure this will sort this problem out. Here's the corrected code:

Swift 3 Version:-

class Visitor: NSObject, NSCoding {
    var name: String = ""
    var contactEmail: String = ""
    var company: String = ""
    var picture: UIImage = UIImage()
    var signature: UIImage = UIImage()
    var timeIn: String = ""
    var timeOut: String = ""
    var signedOut: Bool = false
    var otherQuestionsWithAnswers: [String] = []

func encode(with aCoder: NSCoder) {
//... encode here
}
Dark Innocence
  • 1,389
  • 9
  • 17