Objective C bridging header file not found - ios

I have a class name AppState which is using a swift class object
#import "Sonic-Swift.h"
#interface AppState : NSObject
#property (class) NSMutableArray<"Swift class" *> *entity;
Now I need to use this class (AppState) in the swift file. So I'm trying to import this class in the bridging header.
But after importing this file the app gives an error on the "#import "Sonic-Swift.h"" line "File not found".
failed to emit precompiled header '/Users/krishna_mac_2/Library/Developer/Xcode/DerivedData/Apps-gqelclyzwvyomhcchmjjsejrznaw/Build/Intermediates.noindex/PrecompiledHeaders/Sonic-Bridging-Header-swift_3RJ3MQEOEFTPD-clang_26Q2UBYWMY12Y.pch' for bridging header '/Users/krishna_mac_2/Documents/Documents/Documents/Documents/Documents/Github/Sonic/iOS/Sonic/Sonic-Bridging-Header.h'

import "Sonic-Swift.h"
should be in .m file only.
If you want to use Swift class in .h file then you should use
#class MySwiftClass;

Related

Swift framework - Use Swift class reference in Objective-C class

I am creating Swift framework in which I have to use Objective-C class. So I went through this link. This is the public header of my framework :
#import <UIKit/UIKit.h>
//! Project version number for Test.
FOUNDATION_EXPORT double TestVersionNumber;
//! Project version string for Test.
FOUNDATION_EXPORT const unsigned char TestVersionString[];
// In this header, you should import all the public headers of your framework using statements like #import <Test/PublicHeader.h>
#import <arpa/inet.h>
#import <ifaddrs.h>
#import <netdb.h>
#import <sys/socket.h>
#import <MyTest/MPAppDelegateProxy.h>
Now in class MPAppDelegateProxy, I have to use a Swift class which I have created. This is :
#import "MPAppDelegateProxy.h"
#import "MyTest.h"
#implementation MPAppDelegateProxy
+ (void)proxyAppDelegate {
[MPGlobal MPLog:#"App delegate not set, unable to perform automatic setup." file:#"MPAppDelegateProxy.m" function:#"proxyAppDelegate" line:32];
// rest of code
}
MPGlobal is one of my Swift class. But I am getting :
Use of undeclared identifier 'MPGlobal'
Note : I have added #objC before MPGlobal.
You need to import <Target>-Swift.h file.
This is known as Objective-C Generated Interface Header Name.
You can find it in your Target's build settings.
This file is auto generated by compiler and it needs to be imported in Objective-C files.
change the SWIFT_OBJC_INTERFACE_HEADER_NAME build setting and making it the same across different targets. To do so change the instruction that generates this property from $(SWIFT_MODULE_NAME)-Swift.h to $(PROJECT_NAME)-Swift.h as explained here
After doing this Clean Build Folder by pressing Alt and going into Product menu. Since name of header is shared among targets now it can be imported once in the .m ObjectiveC file and all targets can benefit from Swift classes.
If after building it still shows the error, ensure that the header can be reached from XCode by Cmd clicking on its name. It should open a file that contains code similar to this:
SWIFT_CLASS("_TtC27ProjectName_Summary11MyClass")
#interface MyClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
#end
If need to ensure that those headers are being generated open a terminal and use this command
find ~/Library/Developer/Xcode/DerivedData -name "*Swift.h"
You should see one header for each target
Another issue that happened to me after those changes is that it started giving errors on ObjectiveC code that I didn't touch. The problem was due to the position of the import, as reported here:
Exactly where at the top of a .m file you #import the hidden bridging
header can make a difference. The usual sign of trouble is that you
get an “Unknown type name” compile error, where the unknown type is a
class declared in Objective-C. The solution is to #import the .h file
containing the declaration for the unknown type in your Objective-C
files as well, before you #import the hidden bridging header. Having
to do this can be an annoyance, especially if the Objective-C file in
question has no need to know about this class, but it resolves the
issue and allows compilation to proceed.
At the very end the code compiles and runs on device and simulator!
Original answer
Also you can try this,
You needed to import the -Swift.h for for both the framework and the app target
For Example :
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <Foundation/Foundation.h>
#import "XLPagerTabStrip-Swift.h"
#import "RealmSwift-Swift.h"
...... // Add all frameworks, subclasses, and dependance ios frameworks
#import "MyProject-Swift.h"
You can read this article How to import file header and check paths

Importing ObjC files into Swift

I want to import files from this library, I copied and pasted the following .h and .m files from the ObjC library into my Swift project:
AppDelegate.h AppDelegate.m
ViewController.h ViewController.m
SPUserResizableView.h SPUserResizableView.m
Upon copying and pasting these files into my project, Xcode asked me to create a bridging header file, I then put the following lines into that bridging header file:
#import "ViewController.h"
#import "SPUserResizableView.h"
#import "AppDelegate.h"
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
I tried to build my project, but build failed. I got many unknown type errors and such, which I assume is because the system is not recognizing the ObjC code. I also got this line of error:
failed to emit precompiled header '....../Project-Bridging-Header-swift_24D19ORYKRN0-clang_1KQCLSEAC5RDY.pch' for bridging header '......./Project-Bridging-Header.h'
How do I resolve these errors? What else do I need to do to use the Objective C library in my swift code?
Why you import AppDelegate.h and other
just use this class (SPUserResizableView.h SPUserResizableView.m)
and in Bridge file
^ #import "SPUserResizableView"
in your BridgingHeader just add
#import "SPUserResizableView.h"
dont copy other projects AppDelegate and ViewController. just copy SPUserResizeableView.h and .m file

Expected a type & Failed to import bridging header

I'm working on a project that contains Swift and Objective_C code, it works without any problem until i was trying to import a ".h" class in my "Bridging-Header" file:
Expected a type : in the class that i was trying to import it
Failed to import bridging header '/Users/sysadmin/Desktop/Application /Classes/UI/Application-Bridging-Header.h'
however when i have imported an other class in Bridging Header that works without problem!
There is 3 ways to import objective c file in Swift project.
#import "FileName.h"
#import <FolderName/FileName.h>
#import PackageName; like #import Alamofire;
Try importing your class like
#import "classname.h" if this fails try
#import `<classname/classname.h>`
Remember to clean and then run after you make a change.
Again if it fails check whether you have added the class to your target

Swift Class in objective C

I have settings.swift and i am importing -Swift.h into my ObjC class .m file. I need something like :
#property(nonatomic, strong) Settings *settings;
but -Swift.h must be imported in .m file and .h does not know anything about settings.swift. Do you have idea how to do this ?

#import ProjectName-Swift.h into header file Can't use swift as .h property?

So I'm trying to use an #objc swift class as a property on my objective-c header file.
example
#objc class SwiftClass: NSObject {
}
#interface ObjcObject : NSObject
#pragma mark - Public Properties -
#property (nonatomic, weak) SwiftClass* swiftClass;
However if I #import ProjectName-Swift.h it gives me the error that "'ProjectName-Swift.h'file not found'
I found an answer here that implies you can't actually import Swift into a header file https://stackoverflow.com/a/29951314/3877767
How then am I supposed to use swift code together with objc code? I would not call swift and objc interoperable if you can't use #objc marked swift classes as properties on and objc header file
So inside of my App-Bridging-Header.h
#import "ObjcClass.h"
and inside of my ObjcClass.h
#import "ProjectName-Swift.h"
This gives the error ProjectName-Swift.h can't find file
Either removing the #import ProjectName-Swift.h, or the #import ObjcClass.hfixes the problem, but then I can't use the Swift/OBJC code respectively
You don't import the header file. You give it to the compiler as a so-called "bridging header" that tells the compiler what to share between the two language environments.
You need to put your header file under the Objective-C Bridging Header as shown below:

Resources