Unarchiving .sks file in Swift for iOS 7.1

丶灬走出姿态 提交于 2019-12-13 05:50:50

问题


I have an app which loads nicely from an .sks file in iOS 8 using the following extension:

class func unarchiveFromFile(file : NSString) -> SKNode?
{
    if let path = NSBundle.mainBundle().pathForResource(file as String, ofType: "sks")
    {
        var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

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

However, when I use this with devices running iOS 7.1, I get a EXC_BAD_ACCESS code EXC_I386_GPFLT on the AppDelegate and this in the log:

SKTexture: Error loading image resource: "MissingResource.png"

I believe the issue may be the sks file being improperly loaded. Could anyone tell me how I can load my scene from the sks file?

Thank you for your help,


回答1:


Interestingly I managed to solve this issue by checking whether pre-iOS 8 was running. If so, I would load the game scene and then copy it. This solves the issue somehow:

        let tempScene = GameScene.unarchiveFromFile(gameSceneFile) as? GameScene
        scene = tempScene!.copy() as! GameScene


来源:https://stackoverflow.com/questions/30082825/unarchiving-sks-file-in-swift-for-ios-7-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!