如何将NSCoding应用于课程? [英] How to apply NSCoding to a class?

查看:79
本文介绍了如何将NSCoding应用于课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程.当我尝试将其添加到NSUserDefaults时,如下所示:

I have the following class. When I try to add it to NSUserDefaults like this:

let testClass = TestClass()
testClass.Property1 = 22.3
NSUserDefaults.standardUserDefaults().setObject(testClass, forKey: "object1")

我得到一个错误:

"NSInvalidArgumentException",原因:试图插入非属性 列出关键对象1'的对象

'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object for key object1'

class TestClass: NSObject, NSCoding {
    var Property1:Double?

    override init() {
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        if let priceCoded = aDecoder.decodeObjectForKey("Property1") as? Double {
            self.Property1 = priceCoded
        }
    }

    func encodeWithCoder(aCoder: NSCoder){
        if let priceEncoded = self.Property1 {
            aCoder.encodeObject(priceEncoded, forKey: "Property1")
        }
    }
}

知道我在做什么错吗?

推荐答案

知道我在做什么错吗?

Any idea what I'm doing wrong?

是的.并不是说显而易见的,而是试图插入非属性列表对象"几乎涵盖了它.可以插入默认系统的对象的类型仅限于属性列表类型:NSStringNSDateNSNumberNSData以及集合NSArrayNSDictionary.您显然正在尝试插入其他类型的对象,从而导致错误.

Yes. Not to state the obvious, but "attempt to insert non-property list object" pretty much covers it. The kinds of objects you can insert into the defaults system is limited to property-list types: NSString, NSDate, NSNumber, NSData, and collections NSArray and NSDictionary. You're apparently trying to insert some other kind of object, thus the error.

如果要存储其他类型的对象,则需要以某种方式将其转换为受支持的类型之一.您处在正确的轨道上–这样做的通常方法是使用NSKeyedArchiver序列化对象,该对象可以存储采用NSCoding协议的对象.这个想法是创建一个带密钥的存档器,用它来序列化有问题的对象,然后取回NSData的实例,然后可以存储它.要取回对象,请执行相反的操作:使用NSKeyedUnarchiver将数据对象反序列化为原始对象.

If you want to store some other kind of object, you'll need to somehow transform it into one of the supported types. You're on the right track -- the usual way to do that is to serialize the object using NSKeyedArchiver, which can store objects that adopt the NSCoding protocol. The idea is that you create a keyed archiver, use it to serialize the object(s) in question, and get back an instance of NSData which you can then store. To get the object back, you do the opposite: use a NSKeyedUnarchiver to deserialize the data object back into the original object.

您似乎希望NSUserDefaults为您序列化与NSCoding兼容的对象,但这并没有发生-您需要自己进行操作.

It looks like you were expecting NSUserDefaults to serialize NSCoding-compliant objects for you, but that doesn't happen -- you need to do it yourself.

周围有很多示例,但通常可以找到《归档和序列化编程指南》( ).具体来说,请查看创建和提取档案" 部分,以获取示例代码.以下是相关的序列化示例:

There are plenty of examples around, but the usual place to look for this would be the Archives and Serializations Programming Guide. Specifically, check out the "Creating and Extracting Archives" section for sample code. Here's a piece of the relevant example for serializing:

NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:aPerson forKey:ASCPersonKey];
[archiver finishEncoding];

请记住,为了以此方式序列化对象,该对象必须是采用NSCoding协议的类的实例.同一文档解释了如何实现必要的NSCoding方法,因此在SO上也必须有很多关于它的问题.

Remember that in order to serialize an object this way, that object has to be an instance of a class that adopts the NSCoding protocol. The same document explains how to implement the necessary NSCoding methods, and there must be dozens of questions about it here on SO as well.

这篇关于如何将NSCoding应用于课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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