移动到新场景时接收swift_dynamicCastObjCClassUnconditional错误

时间:2015-02-02 17:47:09

标签: ios swift sprite-kit dynamic-cast

我正在创建一个SpriteKit游戏,并在按下播放按钮后尝试从主视图控制器移动到新场景时收到此错误:

  

swift_dynamicCastObjCClassUnconditional

以下是我的MenuViewController的代码:

import UIKit
import SpriteKit

extension SKNode {
    class func unarchiveFromFile(file : NSString) -> SKNode? {
        if let path = NSBundle.mainBundle().pathForResource(file, 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 PlayScene
            archiver.finishDecoding()
            return scene
        } else {
            return nil
        }
    }
}

class MenuViewController: UIViewController {

    //Variables and Properties
    @IBOutlet weak var playButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func PlayButtonTapped(sender: UIButton) {
if let scene = PlayScene.unarchiveFromFile("PlayScene") as? PlayScene {
            // Configure the view.
            let skView = self.view as SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* 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() -> Int {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Release any cached data, images, etc that aren't in use.
    }

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

这是我的PlayScene(我想要移动的场景)代码:

import SpriteKit
import UIKit

class PlayScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.backgroundColor = UIColor(red: 229/255, green: 190/255, blue: 35/255, alpha: 1.0)
    }


    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {

        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

我在这些文件中没有太多代码,因为我刚开始,但我已经收到此错误!我以前做了很多次,但直到现在我才得到这个错误。以下是调试器所说的内容:

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x10d080620:  pushq  %rbp
0x10d080621:  movq   %rsp, %rbp
0x10d080624:  pushq  %rbx
0x10d080625:  pushq  %rax
0x10d080626:  movq   %rsi, %rcx
0x10d080629:  movq   %rdi, %rbx
0x10d08062c:  xorl   %eax, %eax
0x10d08062e:  testq  %rbx, %rbx
0x10d080631:  je     0x10d08064c               ; swift_dynamicCastObjCClassUnconditional + 44
0x10d080633:  movq   0x82756(%rip), %rsi       ; "isKindOfClass:"
0x10d08063a:  movq   %rbx, %rdi
0x10d08063d:  movq   %rcx, %rdx
0x10d080640:  callq  0x10d0831ca               ; symbol stub for: objc_msgSend
0x10d080645:  testb  %al, %al
0x10d080647:  movq   %rbx, %rax
0x10d08064a:  je     0x10d080653               ; swift_dynamicCastObjCClassUnconditional + 51
0x10d08064c:  addq   $0x8, %rsp
0x10d080650:  popq   %rbx
0x10d080651:  popq   %rbp
0x10d080652:  retq   
0x10d080653:  leaq   0xcdc8(%rip), %rax        ; "Swift dynamic cast failed"
0x10d08065a:  movq   %rax, 0x8ae57(%rip)       ; gCRAnnotations + 8
0x10d080661:  int3   
0x10d080662:  nopw   %cs:(%rax,%rax)

我正在使用新的SpriteKit编辑器,这就是我需要使用unarchiveFromFile方法的原因。正如我在下面的一条评论中提到的,我弄清楚了什么代码有效。但是,我不明白可选项如何影响代码并触发它工作。任何帮助将不胜感激!

谢谢!

0 个答案:

没有答案