I am having trouble with adopting NSSecureCoding. I encode an array containing objects of my custom class, which adopts NSSecureCoding properly. When I decode i
I was really struggling with this for an hour in Swift 5.
My situation was I had a Set of custom Solution objects:
var resultsSet = Set()
Which conformed to Secure Coding:
static var supportsSecureCoding: Bool{ get{ return true } }
Their container object encoded them as an NSSet, because of Secure Coding:
aCoder.encode(resultsSet as NSSet, forKey: "resultsSet")
But I always got a complier error during decoding:
if let decodedResultsSet = aDecoder.decodeObject(of: NSSet.self, forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set
}
Error:
2020-02-11 22:35:06.555015+1300 Exception occurred restoring state value for key 'NS.objects' was of unexpected class 'App.Solution'. Allowed classes are '{( NSSet )}'. 2020-02-11 22:35:06.564758+1300 *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'value for key 'NS.objects' was of unexpected class 'App.Solution'. Allowed classes are '{( NSSet )}'.'
If I changed the decodeObject(ofClass: to Solution:
if let decodedResultsSet = aDecoder.decodeObject(of: Solution.self, forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set
}
I get the error:
2020-02-11 22:33:46.924580+1300 Exception occurred restoring state value for key 'resultsSet' was of unexpected class 'NSSet'. Allowed classes are '{( app.Solution )}'. 2020-02-11 22:33:46.933812+1300 *** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'value for key 'resultsSet' was of unexpected class 'NSSet'. Allowed classes are '{( app.Solution )}'.'
The answer, which is obvious now, but I could find it anywhere was to realise the allowed object list is an array, and it needs both NSSet and the custom object: Solution.
if let decodedResultsSet = aDecoder.decodeObject(of: [NSSet.self, Solution.self], forKey: "resultsSet"){
resultsSet = decodedResultsSet as! Set
}
I hope this helps someone.