objective-c function - How can I import Swift code to Objective-C?
You need to import
TargetName-Swift.h. Note that it's the target 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
If your target name contains spaces, replace them with underscores (e.g.
If your target is a framework, you need to import
Make sure your Swift file is member of the target
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
Here's what to do:
Create a new Project in Objective-C
Create a new
- 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 :
Note: Whenever you want to use your Swift file you must be import following line :
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.
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.
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"
Checkout the pre-release notes about Swift and Objective C in the same project
You should be importing
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.
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