【发布时间】:2016-06-17 01:39:43
【问题描述】:
因此,我正在为我一直在开发的主要游戏制作一些小项目,但不知道为什么,每次我尝试运行我的游戏时,都会出现 SIGABRT 错误提示,并显示以下消息:
* 由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:“* -[NSKeyedUnarchiver decodeObjectForKey:]:无法为键(根)解码类(GameScene)的对象;该类可以在源代码或未链接的库中定义'
我已将以下扩展添加到 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
}
}
我已经调试了代码,它在我添加的扩展类中的以下行崩溃了:
让场景 = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as!游戏场景
并提示上述错误。
我正在运行 Xcode 8 Beta + iOS 10,不,不是因为这个,因为我可以在具有 Beta 版本的此类设备上运行其他项目中使用的完全相同的扩展,而不会出现任何错误。
有什么可能导致这种情况的提示吗?
问候 - 伊万
【问题讨论】:
标签: ios xcode sprite-kit swift3