objective-c function - How can I import Swift code to Objective-C?

7 Answers

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


  • If your target 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 <TargetName/TargetName-Swift.h>

  • Make sure your Swift file is member of the target

file swift.h

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:

  1. Create a new Project in Objective-C

  2. Create a new .swift file  

    • A popup window will appear and ask "Would You like to configure an Objective-C bridging Header".
    • Choose Yes.
  3. Click on your Xcode Project file

  4. Click on Build Settings

  5. Find the Search bar and search for Defines Module.

  6. Change value to Yes.

  7. Search Product Module Name.

  8. Change the value to the name of your project.

  9. 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"

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.

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


Import 'ProjectName(Your Project Name)-Swift.h' in Objective C ".m" file

#import "TestViewController.h"

/*import ProjectName-Swift.h file to access Swift file here*/

#import "ProjectName-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:

  1. Create new Swift file
  2. Xcode will automatically prompt for Bridge-Header file
  3. Generate it
  4. 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")
  5. 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

  1. 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."

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    #import "YourProjectName-Swift.h"


objective-c import swift