21

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?

Andy Tsen
  • 619
  • 2
  • 6
  • 19

5 Answers5

74

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.

Mark Essel
  • 4,606
  • 2
  • 29
  • 47
Nate Cook
  • 92,417
  • 32
  • 217
  • 178
  • 3
    "Yes" seems to be fine. – Carlos Fonseca Jun 14 '14 at 16:19
  • This helped me know where to delete the Bridging Header when FMDB didn't work. – tazboy May 02 '15 at 03:30
  • I had a weird issue where it was trying to load a bridging header that was not found, so i deleted the value in this key and it worked – A'sa Dickens Oct 13 '15 at 11:42
  • In Xcode 8 - instead of "Code Generation" it is in "Swift Compiler-General" – Vivek Bansal Nov 19 '16 at 08:51
  • That worked. I was wondering If say I have class A, B, C in objc. I want to only convert class C to swift for now. Now class C depends on A n B so they goes in Bridging Header. Although class A requires class C. Now I can't import the -Swift.h in A cuz A exists in Bridging Header and that gives obvious error. Anyone solution except to convert class A n B also cuz they are big classes. – Shubham Naik Jun 09 '17 at 05:21
8

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!

Sean
  • 2,018
  • 4
  • 25
  • 32
4

1) create a file called "FMDB-Bridging-Header.h"

inside this file type the following: #import "FMDB.h"

3) go to Build Settings -> Swift Compiler - Code Generation - add to 'Objective-C Bridging Header': FMDB-Bridging-Header.h

or if it was placed inside a folder in your project:

FolderName/FMDB-Bridging-Header.h

K.D
  • 516
  • 3
  • 14
  • 1
    no need to do: "inside this file type the following: #import "FMDB.h"" – Rxxxx May 22 '15 at 13:56
  • 1
    I don't know why this got a negative vote, it actually helped me. I had created a bridged header but later moved it into a sub directory within my project. The Project Build Settings didn't reflect the change, but this answer helped me locate the problem. – CampbellGolf May 22 '15 at 20:15
1
  1. Add a header file to your project with the name "[your-project-name]-Bridging-Header.h

  1. Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes" enter image description here

  2. Go to Build Settings > Linking and add "@executable_path/Frameworks" to Runpath Search Paths enter image description here

Build your project now!

Furqan Khan
  • 504
  • 4
  • 12
0

it could help setting the name of the bridging header with its Project root, as "MyProject/MyProject-Bridging-Header.h" into the string value of the Swift Compiler Build key 'Objective-C Bridging Header'