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.
Related
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
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.
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.
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.
I'm currently trying to add LARSAdController to my iOS project with no success.
As soon as i import the files via #import "LARSAdController.h" in my AppDelegate.h the build process fails and on every occurance of (Class)class in LARSAdController.h i get the cryptic error "Expected identifier". BTW I'm using cocoapods.
Example:
- (void)registerAdClass:(Class)class;
which seems fine to me...
If i create a blank project and import the files they compile, so the problem must be in some relation to my code. Anyone got an idea what may cause this?
Thanks for any help in advance!
class is a reserved word in C++, so I would imagine that some of your project uses Objective-C++.
To solve this, use #import LARSAdController.h in Objective-C implementation files only, and remove its use from header files. You can use #class to forward-declare any occurrences of whatever classes are defined in LARSAdController.h in header files (this is best-practise anyway).
If you need to use LARSAdController from an Objective-C++ class then this is more complicated and you will need to use an Objective-C proxy object or modify their header files (which isn't ideal).