18

I'm integrating Swift code into a large Objective-C project, but I'm running into problems when my Swift code refers to Objective-C classes. For example, suppose I have:

  1. An Objective-C class called MyTableViewController
  2. An Objective-C class called DeletionWorkflow

I declared a Swift class as follows:

class DeletionVC: MyTableViewController {
  let deleteWorkflow: DeletionWorkflow

  ...
}

If I now try to use this class by importing ProjectName-Swift.h into Objective-C code, I get undefined symbol errors for both MyTableViewController and DeletionWorkflow.

I can fix the problem in that individual source file by importing DeletionWorkflow.h and MyTableViewController.h before I import ProjectName-Swift.h but this doesn't scale up to a large project where I want my Swift and Objective-C to interact often.

Is there a way to add forward class references to ProjectName-Swift.h so that these errors don't occur when I try to use Swift classes from Objective-C code in my app?

Bill
  • 44,502
  • 24
  • 122
  • 213

2 Answers2

20

You can create another header file that forward declares or imports the necessary classes, and then imports ProjectName-Swift.h. For example, create a file named ProjectName-Swift-Fixed.h with the contents:

// ProjectName-Swift-Fixed.h

// Forward declarations for property classes
@class DeletionWorkflow;

// Imports for superclasses
#import "MyTableViewController.h";

#import "ProjectName-Swift.h"

Then, instead of #import "ProjectName-Swift.h" in your codebase, use #import "ProjectName-Swift-Fixed.h.

johnboiles
  • 3,494
  • 1
  • 32
  • 26
  • Definitely agree this is a bug. Hopefully Apple makes this easier soon! – johnboiles Jun 12 '14 at 23:01
  • This is definitely the obvious solution - just create a faux umbrella header, as you do for a framework. Yes, they could / should have done it, but it's easy enough to work around given all the other bugs that they should fix first. – Chris Conover Mar 06 '15 at 23:33
  • had more luck with coding #import $(SWIFT_MODULE_NAME)-Swift.h – johndpope Jul 28 '16 at 23:31
  • @johnboiles THANK YOU!!! You definitely helped me a lot. I looking for a solution from two days ago. – Gaby Fitcal Nov 15 '18 at 16:57
17

This is a little silly, but it sounds like your "workaround" is what Apple intended, at least for now. From the interoperability guide:

If you use your own Objective-C types in your Swift code, make sure to import the Objective-C headers for those types prior to importing the Swift generated header into the Objective-C .m file you want to access the Swift code from.

In this devforums thread, someone mentioned they already filed a bug in Radar. You probably should too.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • I wouldn't be surprised if they never fix this. It seems like a hard problem, not easily solved using static techniques like bridging headers. I think the only way would be to get rid of the bridging header mechanism altogether and marry the Swift and Objective-C compilers more closely together. – Echelon Jan 30 '15 at 10:40