如何使用unarchivedObjectOfClass:fromData:error:取消存档数据? [英] How to unarchive data with unarchivedObjectOfClass:fromData:error:?

查看:3756
本文介绍了如何使用unarchivedObjectOfClass:fromData:error:取消存档数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用unarchiveObjectWithDataNSUserDefaults取消存档数据,并且运行良好,但是在iOS 12.0中已弃用. Xcode建议使用unarchivedObjectOfClass:fromData:error:,但此方法无效.

I am using unarchiveObjectWithData to unarchive data from NSUserDefaults and it is working good, but it was deprecated in iOS 12.0. Xcode suggests to use unarchivedObjectOfClass:fromData:error:, but this method does not work.

我有一个要取消存档的类对象数组.

I have an array of objects of my class to unarchive.

我尝试对所有类(NSArray,NSString,MYCLASS ..)使用unarchivedObjectOfClass:fromData:error:,也使用unarchivedObjectOfClasses:fromData:error:

I have tried to use unarchivedObjectOfClass:fromData:error:, also unarchivedObjectOfClasses:fromData:error: with all classes (NSArray, NSString, MYCLASS..)

作品

NSArray *stored = [NSKeyedUnarchiver unarchiveObjectWithData:data];

不是:

NSArray *stored = [NSKeyedUnarchiver unarchivedObjectOfClass:[MYCLASS class] fromData:data error:&error];

我收到数据格式不正确,无法读取."

I am receiving "The data couldn’t be read because it isn’t in the correct format.„

推荐答案

unarchivedObjectOfClass:fromData:error似乎还没有记载,但我已经弄清楚了. 在您取消存档NSArray并假设数组内容是诸如NSString之类的标准类的情况下,这应该对您有用:

unarchivedObjectOfClass:fromData:error seems to be pretty undocumented, but I have figured it out. In your case as you are unarchiving an NSArray and assuming the contents of the array are standard classes like NSString then this should work for you:

NSArray *stored = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:data error:&error];

但是,我取消存档自定义对象PurchasedSubscription时,如果您的NSArray包含任何自定义类,这也适用. 首先,从方法到取消存档的objectOfClass必须是您期望得到的结果的类.

However I was unarchiving a custom object PurchasedSubscription and this also applies if your NSArray contains any custom Classes... Firstly the objectOfClass from the method to unarchive needs to be the class of what you are expecting to be the result.

PurchasedSubscription *purchasedSubscription = [NSKeyedUnarchiver unarchivedObjectOfClass:PurchasedSubscription.class fromData:data error:&error];

接下来,您的自定义类需要符合NSSecureCoding,因此将其添加到类的接口中.我认为您已经实现了NSCoding.

Next, your custom class needs to conform to NSSecureCoding so add this to the interface of the class. I assume you already have NSCoding implemented.

@interface PurchasedSubscription : NSObject <NSCoding, NSSecureCoding>

@end

接下来,您的课程必须覆盖supportsSecureCoding以确认它是否受支持

Next your class has to override supportsSecureCoding to confirm it is supported

+ (BOOL)supportsSecureCoding
{
    return YES;
}

接下来,在initWithCoder:方法中,解码每个属性时需要使用decodeObjectOfClass:key:而不是decodeObjectForKey,再次将Class参数设置为要解码的内容的类类型.

Next, in your initWithCoder: method, you need to use decodeObjectOfClass:key: instead of decodeObjectForKey when decoding each property, again setting the Class parameter as the class type of what's being decoded.

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder
{
    self = [self init];

    if (self)
    {
        ReceiptInfo *receiptInfo = [aDecoder decodeObjectOfClass:[ReceiptInfo class] forKey:@"receiptInfo"];
        return [self initWithReceiptInfo:receiptInfo];
    }

    return self;
}

正如您在此处看到的那样,该类还解码了另一个自定义类ReceiptInfo,因此我必须对该类重复此过程才能使所有功能正常工作.

As you can see here, this class also decodes another custom class ReceiptInfo, so I had to repeat this process with that class to get it all to work.

现在使用时

PurchasedSubscription *purchasedSubscription = [NSKeyedUnarchiver unarchivedObjectOfClass:PurchasedSubscription.class fromData:data error:&error];

它通过安全地解码ReceiptInfo类来安全地解码PurchasedSubscription类,因为它在每个步骤中都知道在解码它之前应该是什么类类型.

it securely decodes the PurchasedSubscription class by securely decoding the ReceiptInfo class as it knows at each step what the class type should be before it decodes it.

对面的NSEncoding上的注释.您需要使用方法

A note on the opposite NSEncoding. You need to use the method

archivedDataWithRootObject:requiringSecureCoding:error:

代替

archivedDataWithRootObject:

使用此方法,您无需传递对象类,而可以传递实际的对象. 就我而言,我像这样创建对象

With this one, you don't pass in the object class, you pass in the actual object. In my case, I create the object like so

PurchasedSubscription *validSub = [[PurchasedSubscription alloc] initWithReceiptInfo:latestReceipt];

然后像这样编码

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:validSub requiringSecureCoding:YES error:&error];

这篇关于如何使用unarchivedObjectOfClass:fromData:error:取消存档数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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