328

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
2

16 Answers 16

519

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

26
  • 5
    Note that if you try to use the Swift filename in your import, you will get the error "Expected ';' after top level declarator". in your Swift file after "import Foundation".
    – louielouie
    Jun 9, 2014 at 22:10
  • 8
    EDIT: Note: for this to work, a project module name MUST be defined. See: stackoverflow.com/a/24064015/2085743 Jul 17, 2014 at 15:34
  • 7
    I just would like to add a note that if your project name has any white space or special character you have to replace them with underscores, like "My App" would be "My_App-Swift.h" Dec 4, 2014 at 4:27
  • 121
    Why is this so hard and poorly documented?
    – uchuugaka
    Nov 1, 2015 at 6:43
  • 8
    I found a new way to find the correct generated header file name: go to your target -> Build Settings -> Search keywords Objective-C Generated Interface Header Name, bingo!
    – Itachi
    Dec 21, 2018 at 3:36
178

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"

9
  • 1
    Changing the value of Defines module in Build settings to Yes is what fixed things for me! Dec 31, 2015 at 13:35
  • Embedded Content Contains Swift : YES USE this also
    – Mohit
    Feb 22, 2016 at 11:05
  • 5
    good answer but steps 7-8 are not needed in Xcode 7.3.1 and higher
    – Sam B
    Jul 17, 2016 at 23:57
  • 1
    Not works for me in xcode 8.3.2. Works in previous versions
    – jose920405
    Sep 5, 2017 at 18:27
  • 2
    Just want to add that YourProjectName in #import "YourProjectName-Swift.h" will be the name of project from step 8. Useful if your project name contains special characters and you are not sure how exactly you should format it. Apr 26, 2019 at 13:32
170

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.

enter image description here

10
  • 2
    Only import "ProductName-Swift.h" in .m files helped me. Adding same in .pch file was giving "Cannot find protocol declaration for 'CLLocationManagerDelegate'; did you mean 'NSLayoutManagerDelegate'?" Aug 13, 2015 at 12:17
  • 1
    @AJit it doesn't directly, the documentation says that. The easiest way is to create an Objective-C class that translates in a format that Objective-C++ understands. developer.apple.com/library/ios/documentation/Swift/Conceptual/… Jan 14, 2016 at 17:05
  • 1
    Note that you have to derive from NSObject !!! you can't just straight up declare a class Blah in swift and have it work. it needs to be class Blah : NSObject
    – David T.
    Dec 8, 2016 at 1:50
  • Worth reading this answer for some cases: stackoverflow.com/questions/26328034/… Depending on what you are doing: #import <ProductName/ProductModuleName-Swift.h> #import <ProductModuleName-Swift.h> #import <NewTestApp/NewTestApp-Swift.h>
    – inigo333
    Apr 3, 2017 at 10:19
  • What if you want import a Swift extension?
    – Ricardo
    Aug 30, 2017 at 23:00
48

If you're using Cocoapods and trying to use a Swift pod in an ObjC project you can simply do the following:

@import <FrameworkName>;

enter image description here

2
  • 1
    Thanks a lot! you saved my day.
    – Esha
    Nov 15, 2017 at 11:12
  • This works like a charm if we installed swift-made library through Cocoapods. Upvoted !!!
    – NSPratik
    Nov 17, 2018 at 10:52
25

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.

17

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.

enter image description here

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
enter image description here

0
15

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.

enter image description here

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"
1
  • The forward declaration (@class) of the class is a very good point! Thanks! Jun 20, 2017 at 7:43
14

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/

3
  • In my code, after I updated it to Swift 4.2 from swift 3. It's not working
    – SNarula
    Mar 3, 2019 at 15:51
  • what error are you getting? I have used above code in Swift4.2 and its working fine. Mar 10, 2019 at 16:23
  • Ok that’s great. May 21, 2020 at 15:49
11

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.

7

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.

Dashes and underscores

5

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"
4
  • 3
    Showed me this error: 'SCLAlertView-Swift.h' file not found. Jun 8, 2014 at 1:32
  • 2
    Needs to be the project name, not the filename - see my answer.
    – Bill
    Jun 8, 2014 at 4:02
  • 1
    Thanks for the documentation link. That was useful for reference.
    – louielouie
    Jun 9, 2014 at 22:09
  • 2
    To quote from the provided link: The name of this header is your product module name followed by adding "-Swift.h". So it's not the class name as you wrote, but the name of your product module and that takes care of importing all swift files, not just a specific one
    – Chris
    May 24, 2015 at 13:41
4

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

1
  • Better than the approved. The dashes wasted 20 min I'll never get back haha.
    – Roi Mulia
    Oct 3, 2017 at 13:27
3

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.

1
  • 2
    i am not getting this option of use legacy swift language version
    – rd_
    Sep 9, 2017 at 7:10
1
#import <TargetName-Swift.h>

you will see when you enter from keyboard #import < and after automaticly Xcode will advice to you.

0

only some tips about syntax, about Xcode everything has been said

  1. you cannot import 'pure" functions, only classes, even if marked "public", so:

    public func f1(){ print("f1"); }

will NOT be called in ANY way.

  1. If You write classes., add inheritance from NSObject, other will NOT be usable.

  2. 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'"

-2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.