简体   繁体   中英

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?:

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
                }
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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