15

I've got a project using the Swift generated Bridging Header and have the project set up correctly (no spaces in Names, Uses Modules, pre-declaring Swift classes in my .mm file, Deleting derived data, doing a clean rebuild etc...). The Bridging Header is being generated fine, but the automatically generated -Swift.h has errors in it. What's even worse, those errors are on the generated (in project creation - Swift) versions of AppDelegate and ViewController which would normally compile fine. The errors in the -Swift.h are:

  • @interface AppDelegate : UIResponder <UIApplicationDelegate>
    >> Cannot find interface declaration for 'UIResponder', superclass of 'AppDelegate'
    >> Cannot find protocol declaration for 'UIApplicationDelegate'
  • @interface ViewController : UIViewController
    >> Cannot find interface declaration for 'UIViewController', superclass of 'ViewController'

I've searched Stack Overflow and the net and can't find any answer that addresses this particular issue. Has anyone else had this? Or, is there a way I can mark my AppDelegate and ViewController classes so Xcode doesn't try and create Objective-C stubs for those Swift classes as I don't actually need them?

5 Answers 5

21

Just add

#import <UIKit/UIKit.h>

before

#import <YourProject-Swift.h>

in .mm file. for me it helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot! This is the only solution that worked for me.
You saved my day. +=1
7

I had same problem and discovered that..

-Swift.h only work best with Objective-C, not Objective-C++ (.mm)

So I use the Objective-C class to connect with the Swift class. Also You can always load your Objective-C++ class in your Objective-C class.

3 Comments

I am having this issue with CoreData :(
for CoreData, I use Objective C to handle the CoreData model class. Feel free to look at my example database app on github SweeneyApps Link at Github
It works for me with Objective-C++ in Xcode 13.
4

Try to import UIKit/UIKit.h into bridging header file.

3 Comments

For me it did not help to import UIKit/UIKit.h in Project-Swift.h, but doing so in my actual Objective C header file did the trick.
If it doesn't work. Try to import UIKit/UIKit.h into bridging header file.
You shouldn't alter the -Swift.h file as this is auto-generated, and the issue will hit you back, once Xcode re-creates this file.
0

Swift doesn’t play very well with Objective-C++. Since I need to interface with C++, I tend to write an Objective-C++ class MyClass_CPP containing just the interface with C++, then an Objective-C subclass MyClass that can play nicely with Swift.

Comments

0

Here's the latest working answer so far (2023).

In my case, this has happened before, when I was working on a chat feature in my Objective-C project.

And now, I want to subclass AVPlayerViewController in Swift, and use that Swift subclass in my Objective-C file.

One way to fix this is to have an Objective-C subclass to subclass the module class of any iOS framework (e.g. AVKit). Then use that Objective-C subclass as the superclass of your Swift subclass.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.