我有麻烦采用NSSecureCoding。我编码了一个包含自定义类对象的数组,它正确地采用了NSSecureCoding
。当我解码它时,传递类NSArray
(这是我编码的对象的类),它抛出一个异常。但是,当对字符串数组执行完全相同的操作时,它工作得很好。我看不出我的类和NSString有什么不同
#import <Foundation/Foundation.h>
@interface Foo : NSObject <NSSecureCoding>
@end
@implementation Foo
- (id)initWithCoder:(NSCoder *)aDecoder {
return [super init];
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
}
+ (BOOL)supportsSecureCoding {
return YES;
}
@end
int main() {
@autoreleasepool {
NSMutableData* data = [[NSMutableData alloc] init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:@[[Foo new]] forKey:@"foo"];
[archiver encodeObject:@[@"bar"] forKey:@"bar"];
[archiver finishEncoding];
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
unarchiver.requiresSecureCoding = YES;
// throws exception: 'value for key 'NS.objects' was of unexpected class 'Foo'. Allowed classes are '{( NSArray )}'.'
[unarchiver decodeObjectOfClass:[NSArray class] forKey:@"foo"];
// but this line works fine:
[unarchiver decodeObjectOfClass:[NSArray class] forKey:@"bar"];
[unarchiver finishDecoding];
}
return 0;
}
你可能已经解决了这个问题,但我只是点击了这个并找到了一个解决方案,我想我会把它留给其他找到这个的人。
我的解决方案是使用decodeObjectOfClasses:forKey:
if let data = defaults.objectForKey(FinderSyncKey) as? NSData
let unArchiver = NSKeyedUnarchiver(forReadingWithData: data)
unArchiver.setRequiresSecureCoding(true)
//This line is most likely not needed, I was decoding the same object across modules
unArchiver.setClass(CustomClass.classForCoder(), forClassName: "parentModule.CustomClass")
let allowedClasses = NSSet(objects: NSArray.classForCoder(),CustomClass.classForCoder())
if let unarchived = unArchiver.decodeObjectOfClasses(allowedClasses, forKey:NSKeyedArchiveRootObjectKey) as? [CustomClass]{
return unarchived
}
}
objective-C中的应该是[unArchiver decodeObjectOfClasses:allowedClasses forKey:NSKeyedArchiveRootObjectKey]
将decode object更改为decode object,为我解决了上述异常。
我在Swift 5中为此挣扎了一个小时。
我的情况是我有一组自定义解决方案对象:
var resultsSet = Set<Solution>()
符合安全编码:
static var supportsSecureCoding: Bool{ get{ return true } }
它们的容器对象将它们编码为NSSet,因为安全编码:
aCoder.encode(resultsSet as NSSet, forKey: "resultsSet")
但是我总是在解码时得到一个编译器错误:
if let decodedResultsSet = aDecoder.decodeObject(of: NSSet.self, forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set<Solution>
}
错误:2020-02-11 22:35:06.555015+1300恢复密钥"NS"的状态值异常。object '是一个意外的类'App.Solution'。允许的类是'{(NSSet)}"。由于未捕获异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'value for key 'NS。object '是一个意外的类'App.Solution'。允许的类是'{(NSSet)} '。'
如果我将decodeObject(ofClass:)改为Solution:
if let decodedResultsSet = aDecoder.decodeObject(of: Solution.self, forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set<Solution>
}
我得到错误:
2020-02-11 22:33:46.924580+1300在恢复键'resultsSet'的状态值时发生异常,异常类'NSSet'。允许的类是'{(app.Solution)}"。由于未捕获异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'resultsSet'键的值属于意外的'NSSet'类。允许的类是'{(app.Solution)} '。'
答案,这是显而易见的,现在,但我可以找到它的任何地方是实现允许的对象列表是一个数组,它需要NSSet和自定义对象:解决方案。
if let decodedResultsSet = aDecoder.decodeObject(of: [NSSet.self, Solution.self], forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set<Solution>
}
我在iOS 15/Xcode 13.2的Swift中遇到了同样的问题。我通过简单地在所需的init(coder decoder: NSCoder){}的decoder. decodeobject中添加NSNumber(警告中列出的违规类)来解决它。(我编辑了类名)。MyVar包含一些数字
required init(coder decoder: NSCoder) {
myVar = decoder.decodeObject(of: [MyType.self, NSNumber.self], forKey: theKey) as? [MyVar] ?? []
}