无法实例化名为SCNView的类

时间:2014-10-26 19:12:48

标签: ios swift sprite-kit

我尝试启动应用时出现此错误。我的应用程序应该只是一个空的SKScene。根据我的理解,这通常是由于没有导入适当的框架。我已经连接了我的UIKit和SpriteKit框架,这些是我正在使用的唯一两个框架。

以下是我的ViewController代码:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    var scene: GameScene!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Configure the view
        let skView = view as SKView
        skView.multipleTouchEnabled = false

        //Create and configure the scene
        scene = GameScene(size: skView.bounds.size)
        scene.scaleMode = .AspectFill

        // Present the scene.
        skView.presentScene(scene)

    }

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


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

}

以下是我的GameScene代码:

import UIKit
import SpriteKit



let HurdleSize:CGFloat = 20.0
let TickLengthLevelOne = NSTimeInterval(600)

class GameScene: SKScene {
    var tick: (() -> ())?
    var tickLengthMillis = TickLengthLevelOne
    var lastTick:NSDate?

    var textureCache = Dictionary<String, SKTexture>()

    override init(size: CGSize) {
        super.init(size: size)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

以下是我的错误跟踪:

  Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named SCNView'
  *** First throw call stack:
  (
    0   CoreFoundation                      0x0161d946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01e1ca97 objc_exception_throw + 44
    2   CoreFoundation                      0x0161d86d +[NSException raise:format:] + 141
    3   UIKit                               0x007bfd74 UINibDecoderDecodeObjectForValue + 317
    4   UIKit                               0x007bfc2f -[UINibDecoder decodeObjectForKey:] + 371
    5   UIKit                               0x00629bf1 -[UIRuntimeConnection initWithCoder:] + 189
    6   UIKit                               0x007bff1a UINibDecoderDecodeObjectForValue + 739
    7   UIKit                               0x007c011c UINibDecoderDecodeObjectForValue + 1253
    8   UIKit                               0x007bfc2f -[UINibDecoder decodeObjectForKey:] + 371
    9   UIKit                               0x00628ea7 -[UINib instantiateWithOwner:options:] + 1164
    10  UIKit                               0x0044b624 -[UIViewController _loadViewFromNibNamed:bundle:] + 270
    11  UIKit                               0x0044bdbb -[UIViewController loadView] + 295
    12  UIKit                               0x0044bfef -[UIViewController loadViewIfRequired] + 78
    13  UIKit                               0x0044c595 -[UIViewController view] + 35
    14  UIKit                               0x00343825 -[UIWindow addRootViewControllerViewIfPossible] + 66
    15  UIKit                               0x00343c99 -[UIWindow _setHidden:forced:] + 287
    16  UIKit                               0x00343f50 -[UIWindow _orderFrontWithoutMakingKey] + 49
    17  UIKit                               0x0035228d -[UIWindow makeKeyAndVisible] + 80
    18  UIKit                               0x002ef776 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3108
    19  UIKit                               0x002f2c0d -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
    20  UIKit                               0x0030b7d0 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
    21  UIKit                               0x002f181f -[UIApplication workspaceDidEndTransaction:] + 155
    22  FrontBoardServices                  0x0745f9de __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
    23  FrontBoardServices                  0x0745f46f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
    24  FrontBoardServices                  0x07471425 __31-[FBSSerialQueue performAsync:]_block_invoke + 26
    25  CoreFoundation                      0x015411c0 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
    26  CoreFoundation                      0x01536ad3 __CFRunLoopDoBlocks + 195
    27  CoreFoundation                      0x0153692b __CFRunLoopRun + 2715
    28  CoreFoundation                      0x01535bcb CFRunLoopRunSpecific + 443
    29  CoreFoundation                      0x015359fb CFRunLoopRunInMode + 123
    30  UIKit                               0x002f11e4 -[UIApplication _run] + 571
    31  UIKit                               0x002f48b6 UIApplicationMain + 1526
    32  HurdleJumpr                         0x000a88cf main + 111
    33  libdyld.dylib                       0x03cbfac9 start + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException

2 个答案:

答案 0 :(得分:9)

当我的项目模板是单一视图应用程序时,我遇到同样的问题。因为Xcode不能自动添加SceneKit.framework。你必须手动添加它。

答案 1 :(得分:1)

您似乎在storyboard中输入了错误的类名。确保storyboard和xib文件中所有自定义类的名称都是正确的。