3

I have an objective-c class that uses swift classes. It all works fine.

I wanted to import the objective-c class into a swift class, so I added its header file to the bridging header. All the sudden I got an error the Projectname_swift.h file is not found.

Any ideas how to resolve this issue? Is it actually possible?

Tibidabo
  • 21,461
  • 5
  • 90
  • 86
  • You don't have to import anything in swift class.. import your objective-c class header in bridging header.... When you are using swift calls in objective-c then you need to import like this `#import "Projectname-Swift.h"` – Bilal May 25 '17 at 03:52
  • @Bilal I don't import any obj-c headers into any swift class. I import swift classes into an obj-c class which in turn I want to use in another swift class by putting its header file in the bridging header. The compiler does not like it. Once I put the header file in the bridging header the _swift.h fails – Tibidabo May 26 '17 at 07:23
  • You need a -, not _, in Projectname_swift.h – mikep Jun 24 '17 at 11:51
  • @mikep it's a typo, the header works fine when I use Swift to Obj-C and Obj-C to Swift but not when Swift to Obj-C to Swift – Tibidabo Jun 25 '17 at 05:23

4 Answers4

5

Let the Xcode build the bridge file from Objective-C to Swift.

Create a temporary directory elsewhere. In there, you create a dummy Xcode Swift project, give the project name the same as your existing Current Project Name. Then add new file, Objective-C (.m file). The XCode will prompt you to create a bridge header file, click on the create bridge file (the right most button).

Now you locate the header file location in Finder. Then drag into your Current Project of Interest, don't forget to checked the copy file if necessary option. Add necessary #import '.....' in the header file.

You should be good. If everything works fine, delete the dummy project.

Gray
  • 346
  • 1
  • 5
5

a circular reference has been created, making it so the Swift code is unable to compile (which leads to the canary error stating that the _Swift.h file is not found).

i have provided a more in depth answer to a similar questions here and here.

long story short, the documentation explicitly says not to this:

To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface.

Forward declarations of Swift classes and protocols can only be used as types for method and property declarations.

in order to make your code compile again you will need to remove the #import "Projectname_Swift.h" line from the offending Objective-C header. ideally you can simply move the import statement into your .m file, however if you need to publicly expose the Swift class in your ObjC header, then you must forward declare it using @class SomeSwiftClass;.

Community
  • 1
  • 1
Casey
  • 6,531
  • 24
  • 43
1

Clean derived data. and then #import "ProjectName-Swift.h" in your objective c files.

  1. Go to Build Settings->Objective-C Generated Interface Header Name and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"
  2. Go to Build Settings and search for "Defines Module", set both values to YES
  3. Create a class in swift with prefix of @objc for example @objc class mySwiftClass{...}
  4. Build the project again
Vibha Singh
  • 623
  • 4
  • 9
  • The project works as long as I don't want to use a objectve-c class that imports a swift class in another swift class. So Swift->OBJ-C = OK, Swift->OBJ-C->Swift = Error – Tibidabo Jun 25 '17 at 05:18
1

it will be better if you use error syntax or screen shot. you can simply try this 1. Goto your project on top of right navigation 2. select build settings from middle pain. 3. search for Objective-C bridging header 4. just below this you will find "Generated interface HeaderName" 5. add correct address of your swift file 6. clean and build the project.

Fahad Malik
  • 51
  • 11