bridging swift and objective-c - ios

I have objective-c project and I added swift files in it. i created bridge file and imported swift file in some header files without problems.
But I need to import some header files to swift files by adding them in the "<project-name>-Bridging-Header.h" file.
If I put header file in that bridge file and this header file was import swift file before; Xcode give me error message: "file not found" for the swift bridge file.
i.e:
I have project name called: "ProjectBlaBla"
I have header file called "readingPage.h"
I have swift file called: "readingSwift.swift"
swift bridge file's name: "ProjectBlaBla-Swift.h"
I created header bridge file: "ProjectBlaBla-Bridging-Header.h"
I imported "ProjectBlaBla-Swift.h" in "readingPage.h" file without problem and used swift classes inside objective-c
when I import "readingPage.h" in the "ProjectBlaBla-Bridging-Header.h", I got error message in "readingPage.h" said: "ProjectBlaBla-Swift.h file not found"
any suggestions ?
thanks

You are not able to reference -Swift.h files directly or indirectly in -Bridging-Header.h files.
If you open -Swift.h, you will see a line near the top, in my case line 99: #import "/Users/.../...-Bridging-Header.h", meaning -Swift.h already imports -Bridging-Header.h, so importing back creates a circular dependency.
To avoid this, any header you import in -Bridging-Header.h must use forward references to Swift classes or protocols it uses as described in answers to this question.
In short, if readingPage.h uses a Swift class named MySwiftClass you should:
Remove any references to -Swift.h from readingPage.h.
Import -Swift.h in readingPage.m
Insert #class MySwiftClass; into readingPage.h before the class is used, letting Objective-C know that such a class exists and is declared elsewhere.

Check whether the bridging header path is correct. On the left, select your project name -> TARGETS -> Build Settings -> search for Objective-C Bridging Header. Refer the photo below.

Two options
Import the "ProjectBlaBla-Swift.h" inside the "readingPage.m" file instead of "readingPage.h" file
Create a new PCH file named "Prefix.pch" and import "ProjectBlaBla-Swift.h" inside the "Prefix.pch" file.
Note: Prefix.pch is a precompiled header which makes compiling faster. You do not need to reimport any files that are imported in prefix.ch file.

Related

How to make custom C code into a SwiftPM package?

I am trying to package C code into a Swift module, let's call it CModule. Once I have put it into the base folder of a project (SwiftModule) and configured the Search Paths, I could get auto-complete to work in the Swift files, as well as detect errors/warnings.
The problem is, it won't recognize the module when it's imported, and Jump to Definition results in the following error:
Couldn't Generate Swift Representation
Error (from SourceKit):
"Could not load module: CModule"
CModule contains the following files:
src folder, containing the implementation and an umbrella header exposing all required functions (by importing other sub-headers) such
that main_header.h imports componentA.h, componentB.h etc
module.modulemap, containing only:
module CModule [system] {
header "src/main_header.h"
export *
}
The CModule folder is then put at the root of the SwiftModule project.
The framework and import search paths are defined as follows: $(PROJECT_DIR)/CModule (recursive)
Considering auto-complete and others work, I am confident the import search path is set correctly. No idea about the framework search path.
Additional details:
There are no spaces anywhere in my paths.
SwiftModule is a "framework" that will eventually be put on CocoaPods.
There are no public headers but the base SwiftModule header. CModule headers are all defined as Project headers.

Xcode : Bridging header not recognising imports

I have a project contains objective C code and I want to implement new features using Swift so I created Bridging header and added it to the build paths, the problem now is when I add #import "xx" to the Bridging header #import is not recognized and when I add it manually by copying and pasting like #import "MBProgressHUD.h", then i try to view the source code I get "?" although when I import the same library in any other .h or .m file it works.
Note: I know that "MBProgressHUD" works with swift, it was just for reference.
Appreciating any support and thanks in advance
I think you should do like this
1、drag the MBProgressHUD into your project
2、you can create an OC class first
, which will remind if you create yourProject-Bridging-Header.h, choose yes
3、import #import "MBProgressHUD.h" in the -Bridging-Header.h,And then compile the project
4、use it

How to access header file declaration without #include

I have an iOS project-ProjectX (not created by me) which is able to access declaration from a .h file without using #include "someHeader.h".
In ProjectX, I could just create an empty File and refer to a declaration in "someHeader.h", which I find perplexing. Example:
#import <Foundation/Foundation.h>
#implementation Empty:NSObject
SOME_TYPE_FROM_SOME_HEADER_H x;
#end
and the compiler automatically knows where the definition is?!
I have since tried to create an identical project, duplicating all the project settings, adding static libraries/files, etc. but to no avail.
Any ideas on what I might have missed out or what do I need to configure in the project to achieve this?
As mentioned by Rishab, I was missing a precompiled header (.pch) file. In the project, a pch imported a static library which contained the header file. Therefore, I was able to call the definitions directly.

How to use swift files in objective c

I want to use my Swift files in Objective-C code. I found different link that say how to do it.
My project name is: Test-Project.
I imported #import "Test-Project-Swift.h" to my .m file to use needed classed in objective c source code.
I watched this video and there is no problem, but I have 'Test-Project-Swift.h' file not found.
As we recognized:
It looks like the problem with "-" we need to change it to "_" - "Test_Project-Swift.h". I've checked Objective-C Generated Interface Header Name in the project settings.

Issue in using swift files into my objC project

I've to use some .swift file into my objctiveC project.when I'm trying to add the swift files into bundle, the productodule-swift.h file not getting generated instead its generating bridging header file.I've changed my project settings DEFINES_MODULE to YES and PRODUCT_MODULE to myprojectnameModule...but no use...is there anything I'm doing wrong.please suggest me...thanks in advance
MyProductName-Bridging-header.h,MyProductName-Swift.h both are generated when importing a .swift file for the first time.
MyProductName-Swift.h file is not visible but exists in project.Never Delete MyProductName-bridging-header.h file
Settings DEFINES_MODULE should be YES and PRODUCT_MODULE should be MyProductName.
Add # import "MyProductName-Swift.h" in your objective-c class.

Resources