简体   繁体   English

archivedObject(ofClass:from:) 对待 [SCNNode] 的方式与 SCNNode 不同

[英]archivedObject(ofClass:from:) treats [SCNNode] differently that SCNNode

I have two methods, one to unarchive a root SCNNode , the other to unarchive an array of root SCNNode 's (within a single file).我有两种方法,一种取消存档根SCNNode ,另一种取消存档根SCNNode的数组(在单个文件中)。 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' .在上面, unarchivedObject(ofClass:from:)有两个错误: Static method 'unarchivedObject(ofClass:from:)' requires that '[SCNNode]' conform to 'NSCoding' and Static method 'unarchivedObject(ofClass:from:)' requires '[SCNNode]' 继承自 '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. print在 else 中打印为“[<SCNNode: 0x600001973600 | no child>]”,调试器显示零元素。

The salient line from the Swift 3 implementation, which worked just fine, is:来自 Swift 3 实现的显着行是:

components = (NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [SCNNode])!

The node files all reside in the Bundle as plists.节点文件都作为 plist 驻留在 Bundle 中。

I've no idea how to make the array version "conform" whereas the single SCNNode does just fine.我不知道如何使数组版本“符合”,而单个 SCNNode 就可以了。

The modern way to decode an array of SCNNode is by saying解码 SCNNode 数组的现代方法是说

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.但我认为会的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM