Swift 编译器-代码生成->Objective-C Briding Header”下设置为 MyProjectName-Bridging-Heade......" /> Swift 编译器-代码生成->Objective-C Briding Header”下设置为 MyProjectName-Bridging-Heade......"> Swift 编译器-代码生成->Objective-C Briding Header”下设置为 MyProjectName-Bridging-Heade......" />
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
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
.
https://i.stack.imgur.com/CF7jn.png
https://i.stack.imgur.com/Bi1tg.png
https://i.stack.imgur.com/nNO3w.png
https://i.stack.imgur.com/quIya.png
https://i.stack.imgur.com/LFGKt.png
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
$(SRCROOT)/$(PROJECT_NAME)/$(SWIFT_MODULE_NAME)-Bridging-Header.h
will be more common :)
$(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.
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.
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.) Delete the derived date. Go to Xcode > Preferences > Locations and click the gray arrow by the Derived Data folder. Then delete your project folder.
https://i.stack.imgur.com/vDTWR.png
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...
https://i.stack.imgur.com/GIlYj.png
Delete the bridging-Header.h
file in Swift Compiler - General
https://i.stack.imgur.com/WmKfk.png
Now you got like this...
https://i.stack.imgur.com/nxpuY.png
These are steps to create Bridging header.
File->New->iOS->Header File, Give the bridging file name as like "yourProjectName-Bridging-Header.h" 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. Build the project, and import necessary files in Bridging header.
To add Bridge File in Swift project.
Step 1. Go to File > Add Cocoa with Objective-C File (For temporary Purpose)
https://i.stack.imgur.com/kHJtM.png
https://i.stack.imgur.com/JJPqK.png
Now press Create Bridging Header Button
DONE YOU WILL GET BRIGDE FILE IN BUNDLE THANKS
For me it helped to use $(SRCROOT)/$(PROJECT_NAME)/ in front of my bridging header file path
For me it helped to use $(SRCROOT)
in front of my Objective-C bridging header path.
$(SRCROOT)/swiftLibraries/swiftLibraries-Bridging-Header.h
Follow the steps below:
Delete bridging header file . . . (Maybe you create manually) and; Create new Swift file in Objective-C Project . . . (Not import, first create it)
Maybe these two things will help solve your problem.
The following worked for me:
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. Correct spelling mistakes in header name. Clear Derived data Target -> Build Settings -> Swift Compiler : General -> Add Obj C Header name. Clean and run again.
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.
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.
What helped me was to move the file manually to the path mentioned in an error message. So:
I deleted the file (moved to trash) Moved it from trash to the path in an error message Later I also had to clean the project
Success story sharing