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
If you accept, Xcode creates the bridging header file along with the file you were creating, and names it by using your product module name followed by "-Bridging-Header. h" . Alternatively, you can create a bridging header yourself by choosing File > New > File > [operating system] > Source > Header File.
Go to Build Settings of your project, find Objective-C Bridging Header row and remove its contents. This works, and in case anyone ends up with a lot of new errors with NSObject after removing this, check to make sure that you have 'import foundation' in your classes that inherit from NSObject.
Its correct to say, Bridging header allows user to use Objective-C classes/files in their swift code in same project. A Swift bridging header allows you to communicate with your old Objective-C classes from your Swift classes. You will need one if you plan to keep portions of your codebase in Objective-C.
Import Swift code into Objective-C within the same framework: Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .
The reason of your issue is, the Xcode build settings still holds the path to auto generated Bridging Header file. You cannot get any build errors because of the header file (ProjectName-Bridging-Header.h) still exist in your project directory.
If Objective-c Bridging Header not found the follow this link stackoverflow.com/questions/38071929/… Than go to Build Settings, find "Objective-C Bridging Header" (as previously guys told) and pass way to your Bridging Header as: YourApp/YourApp-Bridging-Header.h Where 'YourApp/' is a name of app.
Than go to Build Settings, find "Objective-C Bridging Header" (as previously guys told) and pass way to your Bridging Header as: YourApp/YourApp-Bridging-Header.h Where 'YourApp/' is a name of app. It could contain white space, for example 'Your App/'. worked for me. Thank you. Aman Deep, you are welcome!
Change to All and the Swift Compiler - General section should appear with a child setting of "Objective-C Bridging Header" File -> new -> File -> if you want .swift file then click on Swift File .h file click on Header File .m file objective c file etc
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:
I have the name entered correctly in the Build Settings,
but the compiler doesn't find the file.
Therefore, if I add the relative path from the root of the project (that is, I add ./ProjectName/BridgerFileName.h
),
now it compiles and I can call a method in my Objective C class:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With