Xcode : Bridging header not recognising imports - ios

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

Related

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 add files to an already created bridging header?

I currently have a bridging header set up and I want to add more files to it but when I do so I get a "file not found" error:
I know the pod is set up correctly. I'm pretty sure my bridging header is set up correctly. I have it in the MyProject_Showcase-Bridging-Header.h format. It's been added to Targets - Swift Compiler Code Generation. It looks like the first item on the bridging header is working fine. I'm not sure why it can't find the 2nd one.
It looks like your syntax is incorrect. Try replicating the top import.
Something like <fileDirectory/IQDropDownTextfield.h>
Just type in the heather:
#import "file-to-import.h"

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.

bridging swift and objective-c

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.

Expected identifier for every occurance of "(Class)class" in LARSAdController

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).

Resources