Tibidabo
Tibidabo

Reputation: 21571

Adding objective c class that uses swift classes to bridging header Projectname_swift.h not found

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?

Upvotes: 3

Views: 1861

Answers (4)

Casey
Casey

Reputation: 6691

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;.

Upvotes: 5

Gray
Gray

Reputation: 346

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.

Upvotes: 5

Fahad Malik
Fahad Malik

Reputation: 53

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.

Upvotes: 1

Vibha Singh
Vibha Singh

Reputation: 651

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

Upvotes: 1

Related Questions