iosswiftswift3nsuserdefaultsnscoding

Attempt to insert non-property list object when trying to save a custom object in Swift 3


I have a simple object which conforms to the NSCoding protocol.

import Foundation

class JobCategory: NSObject, NSCoding {
    var id: Int
    var name: String
    var URLString: String

    init(id: Int, name: String, URLString: String) {
        self.id = id
        self.name = name
        self.URLString = URLString
    }

    // MARK: - NSCoding
    required init(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id") as? Int ?? aDecoder.decodeInteger(forKey: "id")
        name = aDecoder.decodeObject(forKey: "name") as! String
        URLString = aDecoder.decodeObject(forKey: "URLString") as! String
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(URLString, forKey: "URLString")
    }
}

I'm trying to save an instance of it in UserDefaults but it keeps failing with the following error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object for key jobCategory'

This is the code where I'm saving in UserDefaults.

enum UserDefaultsKeys: String {
    case jobCategory
}

class ViewController: UIViewController {

    @IBAction func didTapSaveButton(_ sender: UIButton) {
        let category = JobCategory(id: 1, name: "Test Category", URLString: "http://www.example-job.com")

        let userDefaults = UserDefaults.standard
        userDefaults.set(category, forKey: UserDefaultsKeys.jobCategory.rawValue)
        userDefaults.synchronize()
    }
}

I replaced the enum value to key with a normal string but the same error still occurs. Any idea what's causing this?


Solution

  • You need to create Data instance from your JobCategory model using JSONEncoder and store that Data instance in UserDefaults and later decode using JSONDecoder.

    struct JobCategory: Codable {
        let id: Int
        let name: String
    }
    
    // To store in UserDefaults
    if let encoded = try? JSONEncoder().encode(category) {
        UserDefaults.standard.set(encoded, forKey: UserDefaultsKeys.jobCategory.rawValue)
    }
    
    // Retrieve from UserDefaults
    if let data = UserDefaults.standard.object(forKey: UserDefaultsKeys.jobCategory.rawValue) as? Data, 
       let category = try? JSONDecoder().decode(JobCategory.self, from: data) {
         print(category.name)
    }
    

    Old Answer

    You need to create Data instance from your JobCategory instance using archivedData(withRootObject:) and store that Data instance in UserDefaults and later unarchive using unarchiveTopLevelObjectWithData(_:), So try like this.

    For Storing data in UserDefaults

    let category = JobCategory(id: 1, name: "Test Category", URLString: "http://www.example-job.com")
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: category, requiringSecureCoding: false)
    let userDefaults = UserDefaults.standard
    userDefaults.set(encodedData, forKey: UserDefaultsKeys.jobCategory.rawValue)
    

    For retrieving data from UserDefaults

    let decoded  = UserDefaults.standard.object(forKey: UserDefaultsKeys.jobCategory.rawValue) as! Data
    let decodedTeams = NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decoded) as! JobCategory
    print(decodedTeams.name)