简体   繁体   English

如何将此经典 Swift 线更新为更新的 Swift(与 Xcode 12 和 Z1BDF6059091920DB1EBZCBDF148 正确兼容)?

[英]How to update this classic Swift line to a more recent Swift (compatible with Xcode 12 and iOS 14) properly?

How to update this classic Swift line to a more recent Swift (compatible with Xcode 12 and iOS 14) properly?:如何将这个经典的 Swift 线更新为更新的 Swift(与 Xcode 12 和 Z1BDF605991920DB1EB1CBD148 兼容)正确?

var sceneData = NSData(bytesNoCopy: path, length: .DataReadingMappedIfSafe, freeWhenDone: true)

Thanks in advance!提前致谢!

I trudged through and found a way, so answering the question:我跋涉并找到了一种方法,所以回答了这个问题:

extension SKNode {
            class func unarchiveFromFile(_ file : String) -> SKNode? {
                if let url = Bundle.main.url(forResource: file, withExtension: "sks") {
                    do {
                        var sceneData = try NSData(contentsOfFile: url.absoluteString, options: NSData.ReadingOptions.mappedIfSafe)
                        var archiver = NSKeyedUnarchiver(forReadingWith: sceneData as Data)

                        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
                        let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! GameScene
                        archiver.finishDecoding()
                        return scene
                    } catch {
                        return nil
                    }
                } else {
                    return nil
                }
            }
}

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

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