13

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?

Richard West
  • 158
  • 2
  • 6

4 Answers4

18

Just add

#import <UIKit/UIKit.h>

before

#import <YourProject-Swift.h>

in .mm file. for me it helps.

Timofey Konshin
  • 201
  • 2
  • 6
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.

Paul Sweeney
  • 71
  • 1
  • 2
4

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

Thao Hoang
  • 63
  • 7
  • 1
    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. – Krøllebølle Dec 10 '16 at 18:13
  • If it doesn't work. Try to import UIKit/UIKit.h into bridging header file. – Thao Hoang May 22 '17 at 09:58
  • 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. – d4Rk Jun 21 '17 at 07:23
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.

gnasher729
  • 49,784
  • 5
  • 72
  • 94