问题
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