Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift "Bridging-Header.h" file not allowing me to instantiate objective-c classes in .swift files

Tags:

When X-code tries to create a bridging header automatically, it crashes every single time, so I followed the instructions on how to manually create a bridging header. (Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?)

Problem is, when I try to instantiate a class in the .swift file that's included in that header, nothing happens (it says that class doesn't exist) Also, in the Bridging Header it doesn't seem to autocomplete my filenames when I try to include them, leading me to believe somethings not linking properly.

Has anyone run into this? Does anyone know how to fix it?

like image 264
Andy Tsen Avatar asked Jun 08 '14 02:06

Andy Tsen


People also ask

How do I create an Objective-C bridging header in Swift?

Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File. Edit the bridging header to expose your Objective-C code to your Swift code: In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.

How do I create an Objective-C bridging header in Xcode?

To create an Objective-C bridging header file, all you need to do is drag some Objective-C code into your Swift project – Xcode should prompt you with the message "Would you like to configure an Objective-C bridging header?" Click "Creating Bridging Header" and you'll see a file called YourProjectName-Bridging-Header.

How do I add Objective-C to Swift?

Add any Objective-C file to your Swift project by choosing File -> New -> New File -> Objective-C File. Upon saving, Xcode will ask if you want to add a bridging header. Choose 'Yes'.


2 Answers

You need to add it to your target's build settings:

  1. In Xcode, if you go into the build settings for your target, and scroll all the way down you'll find a "Swift Compiler - Code Generation" section.

  2. Set "Objective-C Bridging Header" to <#PROJECT_NAME>-Bridging-Header.h

  3. I'm not sure of the correct value for "Install Objective-C Compatibility Header", but it's a yes/no, so you can toggle that if it doesn't work at first.

like image 50
Nate Cook Avatar answered Sep 20 '22 13:09

Nate Cook


I tried to create a bridging header myself but for some reason Xcode didn't like it. So i deleted my custom one, imported an Obj C file which made Xcode ask if I wanted it to create one for me.

I clicked yes, and it worked!

like image 26
Sean Avatar answered Sep 22 '22 13:09

Sean