I have two methods, one to unarchive a root SCNNode
, the other to unarchive an array of root SCNNode
's (within a single file).
This code for the solo node works:
guard let component = try? NSKeyedUnarchiver.unarchivedObject(ofClass: SCNNode.self, from: moleData)
else {
component = failNode
return component // return to moleFunc to abort?
}
But when I try to retrieve an array of nodes, it doesn't work:
guard let components = try? NSKeyedUnarchiver.unarchivedObject(ofClass: [SCNNode].self, from: moleData)
else {
components = []
return components // return to moleFunc to abort?
}
In the above, unarchivedObject(ofClass:from:)
two errors:
Static method 'unarchivedObject(ofClass:from:)' requires that '[SCNNode]' conform to 'NSCoding' and Static method 'unarchivedObject(ofClass:from:)' requires that '[SCNNode]' inherit from 'NSObject'.
The form below will run OK but the operation fails to extract a useable array of nodes:
guard let components = try? (NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: moleData) as! [SCNNode])
else {
print("unarchivedObject(ofClass: FAIL, components = ", components)
components = []
return components // return to moleFunc to abort
}
The print
, within the else, prints out as "[<SCNNode: 0x600001973600 | no child>]", the debugger shows zero elements.
The salient line from the Swift 3 implementation, which worked just fine, is:
components = (NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [SCNNode])!
The node files all reside in the Bundle as plists.
I've no idea how to make the array version "conform" whereas the single SCNNode does just fine.
CodePudding user response:
The modern way to decode an array of SCNNode is by saying
let components = try? NSKeyedUnarchiver.unarchivedArrayOfObjects(ofClass: SCNNode.self, from: moleData)
That should work. If it doesn't, you'll have to just keep using the deprecated method; but I think it will.