129

I want to start using Swift in my Objective-C project. So i added a swift class:

import Foundation
@objc class System : NSObject {
    @objc func printSome() {
        println("Print line System");
    }
}

And imported it into a .m file:

#import "MyProjectName-Swift.h"

When building my project i get the following error:

Bridging header 'PathToMyProject/MyProjectName-Bridging-Header.h' does not exist

NOTE: Under "Build Settings->Swift Compiler - Code Generation->Objective-C Briding Header" is set to MyProjectName-Bridging-Header.h

What should i do to solve this issue?

Any help is much appreciated.

EDIT: Bridging-Header file: #if defined(__has_include) && __has_include() # include #endif

#include <objc/NSObject.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

#if defined(__has_include) && __has_include(<uchar.h>)
# include <uchar.h>
#elif __cplusplus < 201103L
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif
#if !defined(SWIFT_PASTE)
# define SWIFT_PASTE_HELPER(x, y) x##y
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y)
#endif
#if !defined(SWIFT_METATYPE)
# define SWIFT_METATYPE(X) Class
#endif

#if defined(__has_attribute) && __has_attribute(objc_runtime_name)
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X)))
#else
# define SWIFT_RUNTIME_NAME(X)
#endif
#if !defined(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_EXTRA
#endif
#if !defined(SWIFT_CLASS)
# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted) 
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA
# else
#  define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA
# endif
#endif

#if !defined(SWIFT_PROTOCOL)
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA
#endif

#if !defined(SWIFT_EXTENSION)
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__)
#endif

#if !defined(OBJC_DESIGNATED_INITIALIZER)
# if defined(__has_attribute) && __has_attribute(objc_designated_initializer)
#  define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer))
# else
#  define OBJC_DESIGNATED_INITIALIZER
# endif
#endif
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"

#if defined(__has_feature) && __has_feature(modules)
#endif

#pragma clang diagnostic pop
user2408952
  • 2,011
  • 5
  • 24
  • 26
  • 2
    `MyProjectName-Swift.h` and `MyProjectName-Bridging-Header.h` work in opposite directions. `MyProjectName-Swift.h` is provided automatically. `MyProjectName-Bridging-Header.h`, however, is provided semi-automatically, i.e. you have to make sure it's there. So, are you sure that you can see `MyProjectName-Bridging-Header.h` in your project navigator? If not, create and add a file with that name to your project. Then check again that the path to it in the Build Settings is correct (it should include your project directory: `MyProjectName/MyProjectName-Bridging-Header.h`. – Milos Sep 29 '14 at 09:53
  • Have you checked your product module name is not empty? (Build Settings -> Packaging -> Product Module Name) – Nina Sep 29 '14 at 09:56
  • Nina, yes my module name is "MyProjectName" and not something ells – user2408952 Sep 29 '14 at 10:01
  • milos, I thought Xcode was supposed to auto generate this briding file. So no I don't have that file in my project. – user2408952 Sep 29 '14 at 10:02
  • 1
    Have you manually specified the Bridging-Header.h? I see, there is a spelling mistake 'Briding' – Nina Sep 29 '14 at 10:05
  • Nina, no i haven't. I tired adding it as milos suggested but i still get the exact same error. I just checked and that was a spelling mistake from me, in Xcode it is spelled correctly "MtProjectName-Bridging-Header.h" – user2408952 Sep 29 '14 at 10:08
  • Can you delete the value for Objective-C bridging header, clean and execute? – Nina Sep 29 '14 at 10:14
  • Yes that worked, ill try calling my swift function to see if it works. – user2408952 Sep 29 '14 at 10:16
  • Now the error occurs in the .m file where I import the header #import "MyProjectName-Bridging-Header.h" with an error 'MyProjectName-Bridging-Header.h' file not found – user2408952 Sep 29 '14 at 10:19
  • Its 'MyProjectName-Swift.h'.. – Nina Sep 29 '14 at 10:21
  • Do not import `MyProjectName-Bridging-Header` in your .m file – instead import `MyProjectName-Swift.h` – Milos Sep 29 '14 at 10:22
  • Yes, sorry for that. Changed it to "MyProjectName-Swift.h" and now got 0 errors, ill try executing some swift code. – user2408952 Sep 29 '14 at 10:24
  • I can't seem to find my swift class, i tired with 'System *swiftObject = [[System alloc] init];' but i get an error "use of undeclared identifier" – user2408952 Sep 29 '14 at 10:34
  • Command click on `MyProjectName-Swift.h` in the import. This will take you to the header where you can check how, if at all, you class was bridged to objc. – Milos Sep 29 '14 at 10:38
  • It is not listed, so what do i do now? – user2408952 Sep 29 '14 at 10:42
  • Just to add to the previous note: In the `MyProjectName-Swift.h` header, look at the very bottom of the file for `@interface System : NSObject` line. – Milos Sep 29 '14 at 10:43
  • It really should be there. Try a clean build (Shift-Command-K, Command-B), then run... anything? – Milos Sep 29 '14 at 10:44
  • I've updated my post so you can see the file. I've done cleans and builds multiple times, still getting an error when i try to initialize my class in Objective-C – user2408952 Sep 29 '14 at 10:45
  • One more thought, Given the name of your project (`MyProjectName`), it seems you are just playing around and learning, which is great. If this is so, try trashing the whole project and crating a new one, but this time with the experience of having gone through all this trouble already... And make sure you are using the latest XCode Beta build! – Milos Sep 29 '14 at 10:46
  • I'm actually trying to use swift in a really big Objective-C project (Got started many years ago). Can you see any errors in the auto generated header, I updated my post with it? – user2408952 Sep 29 '14 at 10:49
  • Do you have the latest XCode Beta (check on https://developer.apple.com/xcode/downloads/)? My Swift headers look different... – Milos Sep 29 '14 at 10:51
  • I use the released Xcode 6 version not the Xcode 6.1 beta 2 for OS X Yosemite – user2408952 Sep 29 '14 at 10:54
  • Upgrading Xcode might solve your problems. Before that, try setting `Enable Modules (C and Objective-C)` to `Yes` in Build Settings. – Milos Sep 29 '14 at 10:56
  • That one is already set to "Yes" i will try upgrading to 6.1, and return to this post if i still have problems. So if you could take a look on this post in 30min's then i can tell you if it worked. Anyway thanks for all your help so far (also thanks to Nina). – user2408952 Sep 29 '14 at 10:59
  • Ah... sorry. I've no more advice, beyond upgrading Xcode. In the worst case, you could start a new project, work out how to import Swift, and then carefully add all your files to it... This wouldn't be much fun, but it would clean up and modernise your configuration and settings... This last one is not really advice, just a rush thought. – Milos Sep 29 '14 at 11:04
  • Okey i got it working now. I set my Objective-C Bridging Header to "$(PRODUCT_NAME:c99extidentifier)-Bridging-Header.h" and then created a empty header called MyProjectName-Bridging-Header. I can now initialize my swift class and call a function. BIG! Thanks to both of you. – user2408952 Sep 29 '14 at 11:37

12 Answers12

205

If the bridging file is created at the same level as the other classes, you might need to add the relative path, as these pictures show. Note here that the bridging file is created at the same level as the other classes: enter image description here

I have the name entered correctly in the Build Settings, enter image description here

but the compiler doesn't find the file. enter image description here

Therefore, if I add the relative path from the root of the project (that is, I add ./ProjectName/BridgerFileName.h), enter image description here

now it compiles and I can call a method in my Objective C class: enter image description here

James Toomey
  • 5,635
  • 3
  • 37
  • 41
76

I found that after creating the bridging header file manually and choosing the default name and location, the bridging header will be placed in the project directory, which is under the root directory.

This requires the following value for the setting: Targets > [Your App Target] > Build Settings > Swift Compiler - Code Generation > Objective-C Bridging Header:

$(SRCROOT)/$(PROJECT_NAME)/$(PROJECT_NAME)-Bridging-Header.h 

Note that if your project is a swift module (framework) then, as pointed out in the comments, you might prefer:

$(SRCROOT)/$(PROJECT_NAME)/$(SWIFT_MODULE_NAME)-Bridging-Header.h
uɥƃnɐʌuop
  • 14,022
  • 5
  • 58
  • 61
  • 2
    `$(SRCROOT)/$(PROJECT_NAME)/$(SWIFT_MODULE_NAME)-Bridging-Header.h` will be more common :) – gaussblurinc Apr 29 '15 at 15:44
  • 1
    this should be the only answer! – Luca Davanzo Nov 02 '15 at 14:15
  • `$(SRCROOT)/$(PROJECT_NAME)/$(SWIFT_MODULE_NAME)-Bridging-Header.h` upto my project folder I am getting the correct path but appending `/$(PROJECT_NAME)` not taking me to the desired path. – iPeter Jul 14 '17 at 09:56
  • Note for others who might have the same issue - When I copy/pasted @gaussblurinc comment there was some strange character copied in the 'Header' portion of the string. After copy/pasting the same string from the answer, it worked fine. – Adam Johns Oct 06 '17 at 19:53
52

For those who are removing the Bridging Header

I was going the opposite way as most of the other answers here. I had been using a Bridging Header previously, but I didn't need it anymore. After I deleted it from my project I started getting the error mentioned in the question. I performed the following steps to solve my problem.

  1. Go to Targets > [Your App Target] > Build Settings > Swift Compiler - General > Objective-C Bridging Header and delete the path. (Thanks to @Donamite for the idea.) (You can just start typing "bridging" into the search box to find it.)
  2. Delete the derived date. Go to Xcode > Preferences > Locations and click the gray arrow by the Derived Data folder. Then delete your project folder.

enter image description here

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
24

In Swift 4.1

your project you don't have bridging-Header.h file, but your project has that path. For this you need to delete that path...

Go to targets file and select Build Settings, ---->Swift Compiler - General, and delete the bridging-Header.h. Follow below screen shots....enter image description here

Delete the bridging-Header.h file in Swift Compiler - General

enter image description here

Now you got like this...

enter image description here

Naresh
  • 16,698
  • 6
  • 112
  • 113
18

These are steps to create Bridging header.

  1. File->New->iOS->Header File, Give the bridging file name as like "yourProjectName-Bridging-Header.h"

  2. Build-Settings->Objective-C Bridging Header, Just give the bridging header file name be like "ProjectName-Bridging-Header.h"

    NOTE: Bridging header file should be located in the main root folder of the project where the ".xcodeproj" file located. If not move the Bridging header file to the root folder of the project. This way Xcode able to access the bridge file.

  3. Build the project, and import necessary files in Bridging header.

Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
14

To add Bridge File in Swift project.

Step 1. Go to File > Add Cocoa with Objective-C File (For temporary Purpose)

enter image description here

Step 2. Then the following pop up will appearenter image description here

Now press Create Bridging Header Button

DONE
YOU WILL GET BRIGDE FILE IN BUNDLE
THANKS

Maninderjit Singh
  • 1,419
  • 1
  • 13
  • 23
13

For me it helped to use $(SRCROOT)/$(PROJECT_NAME)/ in front of my bridging header file path

  • 1
    This is a valid answer. It should not have been marked down. You can use $(SRCROOT) and $(PROJECT_NAME) as a guide to arrive at the right answer but you probably can't copy and paste every solution. – Hobbes the Tige Oct 12 '15 at 15:37
  • The solution presented in this answer actually has fixed my issue, thanks Srinivas Padidala – Wilson Feb 07 '16 at 16:33
  • Go for it.$(SRCROOT)/$(PROJECT_NAME)/YourProjectName-Bridging-Header.h works for me............ Paste $(SRCROOT)/$(PROJECT_NAME)/ as it is .....do not add your Project name in place of $(PROJECT_NAME) – ioSana Feb 08 '20 at 10:10
6

For me it helped to use $(SRCROOT) in front of my Objective-C bridging header path.

$(SRCROOT)/swiftLibraries/swiftLibraries-Bridging-Header.h
Antoine
  • 23,526
  • 11
  • 88
  • 94
5

Follow the steps below:

  1. Delete bridging header file . . . (Maybe you create manually) and;
  2. Create new Swift file in Objective-C Project . . . (Not import, first create it)

Maybe these two things will help solve your problem.

Blake Mumford
  • 17,201
  • 12
  • 49
  • 67
iHardikTrivedi
  • 559
  • 3
  • 16
5

The following worked for me:

  1. Bridging header file should be located in the main root folder of the project where the ".xcodeproj" file located. Move to project directory and drag and drop bridging header to root if it is inside any other folder.
  2. Correct spelling mistakes in header name.
  3. Clear Derived data
  4. Target -> Build Settings -> Swift Compiler : General -> Add Obj C Header name.
  5. Clean and run again.
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Alvin George
  • 14,148
  • 92
  • 64
2

Creating Bridging Header File Manually

First of all delete the Bridging header file which Xcode is created. And Select your project goto > Build Settings > Search the keyword. Swift Compiler - Code Generation. Click on Objective-C Bridging Header , delete that path. Now clean your Project.

Now select your project > Window in the navigation bar . Select Projects and delete your derived data from there.

Now create a new file , select the Source and then select the Header File and create your Bridging header file . File name must be your projectname-Bridging-Header.h and then create it

Select your project goto > Build Settings > Search the keyword. Swift Compiler - Code Generation. Click on Objective-C Bridging Header and now add the path in this Objective-C Bridging Header like projectname-Bridging-Header.h

Now import your classes into bridging header file and you can compile it your code easily.

Your bridging header file looks like this when you are creating your file manually.

Mandeep Singh
  • 2,810
  • 1
  • 19
  • 31
0

What helped me was to move the file manually to the path mentioned in an error message. So:

  1. I deleted the file (moved to trash)
  2. Moved it from trash to the path in an error message
  3. Later I also had to clean the project
jreft56
  • 199
  • 1
  • 12