Bridging Header Issues - ios

I'm trying to add a Swift file to my app and get a "Failed to import bridging header" error, among others.
Bridging-Header code:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "Menu.h"

In build settings, try setting "Embedded Content contains Swift Code" to Yes.
Also, it looks like its failing to recognize objects from UIKit, so try adding "import UIKit" atop one of your Swift files.

Related

Could not build Objective-C module 'mupdfdk'

I'm trying to add mupdf framework to a swift project. I created a new project and copied the mupdfdk.framework to my project and referenced it in the frameworks section. Following is a screenshot of the documentation.
Now my project looks like this.
Then when I try to import it in viewcontroller, I get Could not build Objective-C module 'mupdfdk' error. Are there any other steps I'm missing? Please help.
You need to add this bridging header:
MuPDF-Bridging-Header.h
#ifndef MuPDF_Bridging_Header_h
#define MuPDF_Bridging_Header_h
#import "mupdfdk.framework/Headers/mupdfdk.h"
#endif /* MuPDF_Bridging_Header_h */
and declare it within the Swift Compiler section of the Build Settings:

Import Obj-C file which imports auto-generated Swift header

This is a partial duplicate of Import a file in bridging-header which imports Swift header but I encounter the same issue as Rich
But what about enums declared in Swift? :(
I am porting an Obj-C iPad app to the iPhone. However I am a Swift developer who would really rather not rewrite existing functionality; replacing the UI instead.
I created a new target for the iPhone version. In my bridging header I import an obj-c class that uses #import "ProjectName-Swift.h". Since this file is autogenerated it doesn't exist when I build this new target. The linked answer is to add a #class but the legacy code makes use of an enum which is now giving the error "Expected a type".
// File that I am currently importing
-(void)setSmileyType:(SmileyFace)type andDelegate:(id<NumberRatingDelegate>)delegate;
// This line now throws an error "Expected a type"
//File that was previously auto imported
#objc public enum SmileyFace: Int {
#objc enum in Swift is exposed as a C-enum in ProjectName-Swift.h .
(Using a macro SWIFT_ENUM.)
You can put something like this into your Objective-C header files which are using the Swift enum:
typedef enum SmileyFace: NSInteger SmileyFace;
(Same as the first part of the generated code with the macro SWIFT_ENUM.)

iOS Swift project, have Objective-C file and I want to import a swift class to the Objective-C file [duplicate]

I have written a library in Swift and I wasn't able to import it to my current project, written in Objective-C.
Are there any ways to import it?
#import "SCLAlertView.swift" - 'SCLAlertView.swift' file not found
You need to import ProductName-Swift.h. Note that it's the product name - the other answers make the mistake of using the class name.
This single file is an autogenerated header that defines Objective-C interfaces for all Swift classes in your project that are either annotated with #objc or inherit from NSObject.
Considerations:
If your product name contains spaces, replace them with underscores (e.g. My Project becomes My_Project-Swift.h)
If your target is a framework, you need to import <ProductName/ProductName-Swift.h>
Make sure your Swift file is member of the target
Here's what to do:
Create a new Project in Objective-C
Create a new .swift file
 
A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
Choose Yes.
Click on your Xcode Project file
Click on Build Settings
Find the Search bar and search for Defines Module.
Change value to Yes.
Search Product Module Name.
Change the value to the name of your project.
In App delegate, add the following : #import "YourProjectName-Swift.h"
Note: Whenever you want to use your Swift file you must be import following line :
#import "YourProjectName-Swift.h"
Instructions from the Apple website:
To import Swift code into Objective-C from 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 .m file within that
framework target using this syntax and substituting the appropriate
names:
#import "ProductName-Swift.h"
Revision:
You can only import "ProductName-Swift.h" in .m files.
The Swift files in your target will be visible in Objective-C .m files
containing this import statement.
To avoid cyclical references, don’t import Swift into an Objective-C
header file. Instead, you can forward declare a Swift class to use it
in an Objective-C header. Note that you cannot subclass a Swift class
in Objective-C.
If you're using Cocoapods and trying to use a Swift pod in an ObjC project you can simply do the following:
#import <FrameworkName>;
Go to build settings in your project file and search for "Objective-C Generated Interface Header Name. The value of that property is the name that you should include.
If your "Product Module Name" property (the one that the above property depends on by default) varies depending on whether you compile for test/debug/release/etc (like it does in my case), then make this property independent of that variation by setting a custom name.
Importing Swift file inside Objective-c can cause this error, if it doesn't import properly.
NOTE: You don't have to import Swift files externally, you just have to import one file which takes care of swift files.
When you Created/Copied Swift file inside Objective-C project. It would've created a bridging header automatically.
Check Objective-C Generated Interface Header Name at Targets -> Build Settings.
Based on above, I will import KJExpandable-Swift.h as it is.
Your's will be TargetName-Swift.h, Where TargetName differs based on your project name or another target your might have added and running on it.
As below my target is KJExpandable, so it's KJExpandable-Swift.h
First Step:-
Select Project Target -> Build Setting -> Search('Define') -> Define Module
update value No to Yes
"Defines Module": YES.
"Always Embed Swift Standard Libraries" : YES.
"Install Objective-C Compatibility Header" : YES.
Second Step:-
Add Swift file Class in Objective C ".h" File as below
#import <UIKit/UIKit.h>
#class TestViewController(Swift File);
#interface TestViewController(Objective C File) : UIViewController
#end
Import 'ProjectName(Your Project Name)-Swift.h' in Objective C ".m" file
//TestViewController.m
#import "TestViewController.h"
/*import ProjectName-Swift.h file to access Swift file here*/
#import "ProjectName-Swift.h"
If you have a project created in Swift 4 and then added Objective-C files, do it like this:
#objcMembers
public class MyModel: NSObject {
var someFlag = false
func doSomething() {
print("doing something")
}
}
Reference: https://useyourloaf.com/blog/objc-warnings-upgrading-to-swift-4/
There's one caveat if you're importing Swift code into your Objective-C files within the same framework. You have to do it with specifying the framework name and angle brackets:
#import <MyFramework/MyFramework-Swift.h>
MyFramework here is the "Product Module Name" build setting (PRODUCT_NAME = MyFramework).
Simply adding #import "MyFramework-Swift.h" won't work. If you check the built products directory (before such an #import is added, so you've had at least one successful build with some Swift code in the target), then you should still see the file MyFramework-Swift.h in the Headers directory.
Be careful with dashes and underscores, they can be mixed up and your Project Name and Target name won't be the same as SWIFT_MODULE_NAME.
Checkout the pre-release notes about Swift and Objective C in the same project
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75
You should be importing
#import "SCLAlertView-Swift.h"
Search for "Objective-C Generated Interface Header Name" in the Build Settings of the target you're trying to build (let's say it's MyApp-Swift.h), and import the value of this setting (#import "MyApp-Swift.h") in the source file where you're trying to access your Swift APIs.
The default value for this field is $(SWIFT_MODULE_NAME)-Swift.h. You can see it if you double-click in the value field of the "Objective-C Generated Interface Header Name" setting.
Also, if you have dashes in your module name (let's say it's My-App), then in the $(SWIFT_MODULE_NAME) all dashes will be replaced with underscores. So then you'll have to add #import "My_App-Swift.h".
If you want to use Swift file into Objective-C class, so from Xcode 8 onwards you can follow below steps:
If you have created the project in Objective-C:
Create new Swift file
Xcode will automatically prompt for Bridge-Header file
Generate it
Import "ProjectName-Swift.h" in your Objective-C controller (import in implementation not in interface) (if your project has space in between name so use underscore "Project_Name-Swift.h")
You will be able to access your Objective-C class in Swift.
Compile it and if it will generate linker error like: compiled with newer version of Swift language (3.0) than previous files (2.0) for architecture x86_64 or armv 7
Make one more change in your
Xcode -> Project -> Target -> Build Settings -> Use Legacy Swift Language Version -> Yes
Build and Run.
#import <TargetName-Swift.h>
you will see when you enter from keyboard #import < and after automaticly Xcode will advice to you.
only some tips about syntax, about Xcode everything has been said
you cannot import 'pure" functions, only classes, even if marked "public", so:
public func f1(){
print("f1");
}
will NOT be called in ANY way.
If You write classes., add inheritance from NSObject, other will NOT be usable.
if it inherits from NSObject, as below:
class Utils : NSObject{
static func aaa()->String{
return "AAA"
}
#objc static func bbb()->String{
return "BBB"
}
#objc private static func ccc()->String{
return "CCC"
}
}
in OBJC:
aaa() NOT called: "No known class method for selector 'aaa'"
bbb() ok
ccc() NOT called: "No known class method for selector 'aaa'"
Find the .PCH file inside the project. and then add #import "YourProjectName-Swift.h" This will import the class headers. So that you don't have to import into specific file.
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iPhone SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "YourProjectName-Swift.h"
#endif

Use of undeclared type 'RoboViewController' Swift

I install RoboReaderPDF framework via pod in my project and I imported it in my Bridging-Header.h file
the issue is when I am trying to instantiate the RoboViewController I am getting this error
Use of undeclared type 'RoboViewController'
any help!?
Looks like your bridging header has not been set up correctly. It should have the following structure.
#ifndef Bridging_Header_h
#define Bridging_Header_h
#import <RoboReaderPDF/RoboReader.h>
#endif
Importing the RoboReader.h file will also import the view controller (check file contents).
Additionally, also check that your bridging header has been specified under Objective-C Bridging Header key in Swift Compiler - Code Generation segment in Build settings.

'ProductModuleName-Swift.h' file not found

I want to use my protocol method of swift class into objective-c class for it I have to import 'ProductModuleName-Swift.h' and declare its protocol in interface but it shows error: 'ProductModuleName-Swift.h' file not found.
But when I import it into test.m file then its fine but when I import it into test.h then it shows the error.
example:
test.h
#import "myProject-Swift.h"
#interface test : UIViewController<mySwiftDelegate>
or There is any way to use protocol method of swift in obj-c class
thanks in advance.
Go to Targets -> Build Settings -> Swift Compiler - Code Generation and make sure the "Objective-C Bridging Header" points to the correct place and the "Objective-C Generated Interface Header Name" is the correct file. My header name for the unit tests defaulted to Module-NameTest-swift.h I needed to remove "Test" from the title.

Resources