NSInvalidUnarchiveOperationException在iOS 9.3上使用SpriteKit

时间:2016-06-17 01:39:44

标签: ios xcode sprite-kit swift3

所以我正在为一个我一直在开发的主要游戏制作一些小项目,但不知道为什么,每当我尝试运行我的游戏时,SIGABRT错误会提示以下消息:

  

*由于未捕获的异常'NSInvalidUnarchiveOperationException'终止应用程序,原因:'* - [NSKeyedUnarchiver decodeObjectForKey:]:无法解码类(GameScene)的对象的密钥(root);该类可以在源代码中定义,也可以在未链接的库中定义

我已将以下扩展名添加到SKNode

    extension SKNode {
    class func unarchiveFromFile(_ file: NSString) -> SKNode? {
        if let path = Bundle.main().pathForResource(file as String, ofType: "sks") {
            do {
                let sceneData = try Data(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.dataReadingMappedIfSafe)
                let archiver = NSKeyedUnarchiver(forReadingWith: sceneData)
                archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
                let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! GameScene
                archiver.finishDecoding()
                return scene
            } catch {
                print("ERROR1002")
                return nil
            }
        } else {
            print("ERROR001 - pathForResource not found")
            return nil
        }
    }
}

我在其他项目中使用过此扩展程序,所有内容都像魅力一样,但现在就在这个项目上。

当我的游戏登陆时,我的GameViewController.swift文件被调用,在我的 viewDidLoad 方法中,我执行以下操作:

import UIKit
import SpriteKit
import GameplayKit




class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let scene = GameScene.unarchiveFromFile(currentSKSFile) as? GameScene {

            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = false
            skView.showsNodeCount = false

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .aspectFill

            skView.presentScene(scene)
        }
    }


    override func shouldAutorotate() -> Bool {
        return true
    }

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        if UIDevice.current().userInterfaceIdiom == .phone {
            return .landscape
        } else {
            return .all
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
        print("didReceiveMemoryWarning ERRO - GameViewController.swift")
    }

    override func prefersStatusBarHidden() -> Bool {
        return true
    }

}

我调试了代码,它在我添加的扩展类中的以下行崩溃了:

  

让scene = archiver.decodeObject(forKey:NSKeyedArchiveRootObjectKey)为! GameScene

它会提示上述错误。

我正在运行Xcode 8 Beta + iOS 10,不,不是因为这个原因,因为我可以运行与此类设备上其他项目完全相同的扩展程序,并且没有任何错误。

对可能导致此问题的任何暗示?

问候 - 伊万

1 个答案:

答案 0 :(得分:0)

以某种方式清理我的项目解决了我的问题!

我尝试了不同的东西,错误就在那里,所以我已经清理过了,错误就不再存在了。

我不知道为什么会这样,但它已经解决了。

非常感谢:)

相关问题