Hatchmaster J
Hatchmaster J

Reputation: 561

Swift error: "Cannot find interface declaration for 'SKScene', superclass of"

Disclaimer: I read through similar questions on SO, couldn't find a solution to my problem.

I want to add an subclass of SKScene to a project that did not previously use SpriteKit.

The project is a mixture of ObjC and Swift. The subclass is written in Swift.

What I did:

  1. Added SpriteKit.Framework by going to the project -> Build Phases -> Link Binary with Libraries .
  2. I created a swift class and had it inherit from SKScene.

Here is the new class:

import SpriteKit

class SnowScene: SKScene {
}

Note that the class is not references by anywhere else in the code.

When I compile my app, I get the error in the title. The error occurs in my -Swift.h file, which auto-exported the new class (I have no idea why).

SWIFT_CLASS("_TtC11MyCoolApp9SnowScene")
@interface SnowScene : SKScene
@end

Does anyone have any idea what am I doing wrong and how to circumvent this problem?

Upvotes: 4

Views: 4352

Answers (1)

Brian Nickel
Brian Nickel

Reputation: 27560

I'm assuming you did import SpriteKit in your source file or you would have gotten an error in the Swift compile phase.

This sounds like a bug in Swift where it's not correctly detecting that you are using SpriteKit. Normally, these dependencies are detected and inserted at the top of -Swift.h in an import section, around line 88:

#if defined(__has_feature) && __has_feature(modules)
@import Foundation;
@import UIKit;
@import ObjectiveC;
...
#endif

If SpriteKit isn't in that list, you're looking at a compiler bug and should file a radar with Apple.

If you have a bridging header, you will notice that right below those imports is:

#import "/Users/.../...-Bridging-Header.h"

If not, you should add one. I believe adding a single Objective-C file will trigger Xcode to do the work for you.

Including the line #import <SpriteKit/SpriteKit.h> in your bridging header will ensure that SpriteKit gets imported before your class is declared.

Upvotes: 6

Related Questions