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

6

101

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?

Sero answered 28/12, 2016 at 4:38 Comment(2)
I created a similar question asking where this is documented (online or in howto books). Did you find a resource for this?Desmond
userDefaults.synchronize is unnecessary and shouldn't be used.Alika
G
136

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)
Greatniece answered 28/12, 2016 at 5:7 Comment(7)
I am trying to do this on an NSArchiveObject but its resulting in errorEcumenical
I created a similar question asking where this is documented (online or in howto books). Did you find a resource for this?Desmond
NSKeyedArchiver can throw an error so it should be marked with 'try' and handle errors as well.Veinstone
I am trying the storing method, and I'm getting the following error: The data couldn’t be written because it isn’t in the correct format.Dougall
The "Archives and Serialization Programming Guide" (developer.apple.com/library/archive/documentation/Cocoa/…) specifies: "Only objects whose class conforms to the NSCoding protocol can be written to an archive." (this doc isn't referenced directly from the NSKeyedArchiver reference, and it took me a bit of digging to find it). So in order to archive your custom object you must implement encodeWithCoder: and initWithCoder:.Charkha
This answer is useful if you can implement the correct protocol, but if you already have a good JSON en/decoding framework in place, like you get with Swift, you'd probably be better using that.Charkha
For consistency though, in your example of retrieving it should be let encodedData = UserDefaults' or let encodedCategory =`, because it was encoded when saved and encoded when retrieved, until you decode it with the unarchiver.Autotrophic
C
48

Update Swift 4, Xcode 10

I have written a struct around it for easy access.

//set, get & remove User own profile in cache
struct UserProfileCache {
    static let key = "userProfileCache"
    static func save(_ value: Profile!) {
         UserDefaults.standard.set(try? PropertyListEncoder().encode(value), forKey: key)
    }
    static func get() -> Profile! {
        var userData: Profile!
        if let data = UserDefaults.standard.value(forKey: key) as? Data {
            userData = try? PropertyListDecoder().decode(Profile.self, from: data)
            return userData!
        } else {
            return userData
        }
    }
    static func remove() {
        UserDefaults.standard.removeObject(forKey: key)
    }
}

Profile is a Json encoded object.

struct Profile: Codable {
let id: Int!
let firstName: String
let dob: String!
}

Usage:

//save details in user defaults...
UserProfileCache.save(profileDetails)

Hope that helps!!!

Thanks

Craftsman answered 11/10, 2018 at 7:13 Comment(1)
Outstanding .....Dostie
A
19

Swift save Codable object to UserDefault with @propertyWrapper

@propertyWrapper
    struct UserDefault<T: Codable> {
        let key: String
        let defaultValue: T

        init(_ key: String, defaultValue: T) {
            self.key = key
            self.defaultValue = defaultValue
        }

        var wrappedValue: T {
            get {

                if let data = UserDefaults.standard.object(forKey: key) as? Data,
                    let user = try? JSONDecoder().decode(T.self, from: data) {
                    return user

                }

                return  defaultValue
            }
            set {
                if let encoded = try? JSONEncoder().encode(newValue) {
                    UserDefaults.standard.set(encoded, forKey: key)
                }
            }
        }
    }




enum GlobalSettings {

    @UserDefault("user", defaultValue: User(name:"",pass:"")) static var user: User
}

Example User model confirm Codable

struct User:Codable {
    let name:String
    let pass:String
}

How to use it

//Set value 
 GlobalSettings.user = User(name: "Ahmed", pass: "Ahmed")

//GetValue
print(GlobalSettings.user)
Alysonalysoun answered 9/4, 2020 at 22:53 Comment(2)
THAT LOOKS LIKE SOME Voodoo magic ✨ But I like it!Abroach
Someone was having fun with property wrappers lol. Cheers.Dougall
S
15

Save dictionary Into userdefault

let data = NSKeyedArchiver.archivedData(withRootObject: DictionaryData)
UserDefaults.standard.set(data, forKey: kUserData)

Retrieving the dictionary

let outData = UserDefaults.standard.data(forKey: kUserData)
let dict = NSKeyedUnarchiver.unarchiveObject(with: outData!) as! NSDictionary
Suet answered 21/9, 2018 at 10:52 Comment(1)
I created a similar question asking where this is documented (online or in howto books). Did you find a resource for this?Desmond
F
3

Based on Harjot Singh answer. I've used like this:

struct AppData {

    static var myObject: MyObject? {

        get {
            if UserDefaults.standard.object(forKey: "UserLocationKey") != nil {
                if let data = UserDefaults.standard.value(forKey: "UserLocationKey") as? Data {
                    let myObject = try? PropertyListDecoder().decode(MyObject.self, from: data)
                    return myObject!
                }
            }
            return nil
        }

        set {
            UserDefaults.standard.set(try? PropertyListEncoder().encode(newValue), forKey: "UserLocationKey")
        }

    }
}
Fraley answered 25/3, 2020 at 15:13 Comment(0)
L
1

Here's a UserDefaults extension to set and get a Codable object, and keep it human-readable in the plist (User Defaults) if you open it as a plain text file:

extension Encodable {
    var asDictionary: [String: Any]? {
        guard let data = try? JSONEncoder().encode(self) else { return nil }
        return try? JSONSerialization.jsonObject(with: data) as? [String : Any]
    }
}

extension Decodable {
    init?(dictionary: [String: Any]) {
        guard let data = try? JSONSerialization.data(withJSONObject: dictionary) else { return nil }
        guard let object = try? JSONDecoder().decode(Self.self, from: data) else { return nil }
        self = object
    }
}

extension UserDefaults {
    func setEncodableAsDictionary<T: Encodable>(_ encodable: T, for key: String) {
        self.set(encodable.asDictionary, forKey: key)
    }

    func getDecodableFromDictionary<T: Decodable>(for key: String) -> T? {
        guard let dictionary = self.dictionary(forKey: key) else {
            return nil
        }
        return T(dictionary: dictionary)
    }
}

If you want to also support array (of codables) to and from plist array, add the following to the extension:

extension UserDefaults {
    func setEncodablesAsArrayOfDictionaries<T: Encodable>(_ encodables: Array<T>, for key: String) {
        let arrayOfDictionaries = encodables.map({ $0.asDictionary })
        self.set(arrayOfDictionaries, forKey: key)
    }

    func getDecodablesFromArrayOfDictionaries<T: Decodable>(for key: String) -> [T]? {
        guard let arrayOfDictionaries = self.array(forKey: key) as? [[String: Any]] else {
            return nil
        }
        return arrayOfDictionaries.compactMap({ T(dictionary: $0) })
    }
}

If you don't care about plist being human-readable, it can be simply saved as Data (will look like random string if opened as plain text):

extension UserDefaults {
    func setEncodable<T: Encodable>(_ encodable: T, for key: String) throws {
        let data = try PropertyListEncoder().encode(encodable)
        self.set(data, forKey: key)
    }

    func getDecodable<T: Decodable>(for key: String) -> T? {
        guard
            self.object(forKey: key) != nil,
            let data = self.value(forKey: key) as? Data
        else {
            return nil
        }

        let obj = try? PropertyListDecoder().decode(T.self, from: data)
        return obj
    }
}

(With this second approach, you don't need the Encodable and Decodable extensions from the top)

Loathly answered 19/4, 2021 at 11:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.