尝试在 Swift 3 中保存自定义对象时尝试插入非属性列表对象 [英] Attempt to insert non-property list object when trying to save a custom object in Swift 3

查看:19
本文介绍了尝试在 Swift 3 中保存自定义对象时尝试插入非属性列表对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个符合 NSCoding 协议的简单对象.

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")
    }
}

我正在尝试在 UserDefaults 中保存它的一个实例,但它一直失败并出现以下错误.

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

由于未捕获的异常NSInvalidArgumentException"而终止应用,原因:尝试为关键 jobCategory 插入非属性列表对象"

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

这是我在 UserDefaults 中保存的代码.

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?

推荐答案

您需要使用 archivedData(withRootObject:) 并存储该DataUserDefaults 中的实例,然后使用 unarchiveTopLevelObjectWithData(_:),所以试试这个.

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.

用于在 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)

用于从 UserDefaults

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

这篇关于尝试在 Swift 3 中保存自定义对象时尝试插入非属性列表对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆