iosobjective-csprite-kit

SpriteKit view throwing NSInvalidArgumentException


I have this code below that loads a scene in the viewDidLoad: method.

SKView *spriteView = (SKView *) self.view;
spriteView.showsDrawCount = YES;
spriteView.showsNodeCount = YES;
spriteView.showsFPS = YES;
PFPiePlanesScene *scene = [PFPiePlanesScene sceneWithSize:self.view.frame.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[spriteView presentScene:scene];

When I call anything on the spriteView, it crashes with this message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsDrawCount:]: unrecognized selector sent to instance 0x9685030'

I assume this is because it is not treating the instance as a SKView and instead as a UIView.

Sprite kit uses this code to load a scene.

// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = NO;
skView.showsNodeCount = NO;

// Create and configure the scene.
self.scene = [SKMyScene sceneWithSize:skView.bounds.size];
self.scene.scaleMode = SKSceneScaleModeAspectFill;

// Present the scene.
[skView presentScene:self.scene];

And, with no error. The classes are the exact same. Would just coming into the view screw this up?


Solution

  • self.view is a UIView object. Simply casting the pointer does not make it a different type of object.

    SKView *spriteView = (SKView *) self.view;
    

    spriteView is now pointing to the object, that self.view is pointing to, telling the compiler, that it actually points to an object of type SKView. That's why you don't get any compiler errors or warnings. At runtime you are sending messages that can't be handled by spriteView because it is still just a UIView object.