By Viktor Radchenko


2014-06-08 01:08:00 8 Comments

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

15 comments

@Erhan Demirci 2019-08-08 13:15:35

#import <TargetName-Swift.h>

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

@Naloiko Eugene 2019-06-05 05:26:03

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

@Prashant Bhayani 2018-08-31 15:40:05

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/

@SNarula 2019-03-03 15:51:26

In my code, after I updated it to Swift 4.2 from swift 3. It's not working

@Prashant Bhayani 2019-03-10 16:23:26

what error are you getting? I have used above code in Swift4.2 and its working fine.

@Bill 2014-06-08 04:01:24

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.

Considerations:

  • 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

@louielouie 2014-06-09 22:10:46

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

@Ruben Martinez Jr. 2014-07-17 15:22:27

Wow, great catch! Small question: Is there anywhere I can see what that project name is? I tried what you suggested and am still getting errors so I'm worried I might have the project name wrong.

@Ruben Martinez Jr. 2014-07-17 15:34:04

EDIT: Note: for this to work, a project module name MUST be defined. See: stackoverflow.com/a/24064015/2085743

@Hola Soy Edu Feliz Navidad 2014-12-04 04:27:13

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"

@Hardik Amal 2015-09-07 14:50:08

Hi @Bill how do i use swift library through cocoapod in my objective c project...?

@marchinram 2015-09-14 22:42:32

How does this work in a Framework target, can you just add #import <ProjectName/ProjectName-Swift.h> to the umbrella header?

@uchuugaka 2015-11-01 06:43:08

Why is this so hard and poorly documented?

@Raptor 2015-12-21 09:53:04

@HardikAmal you can still use, for example, #import "SCLAlertView-Swift.h" in Objective-C class, or @import <SCLAlertView>;

@Amit Saxena 2016-06-29 07:49:49

@uchuugaka it's not that much hard. First time you can feel a bit pain and then later you would be getting mastery on it.

@Maria 2016-10-14 11:34:38

Thanks! That really helps!

@Aamir 2017-04-22 06:41:50

I have used #import "SCLAlertView-Swift.h" in .m and I getting error: SCLAlertView-Swift.h not found, I did something wrong?

@Bill 2017-04-22 12:11:48

@Aamir Yes, re-read my answer

@Aamir 2017-04-22 13:33:04

@Bill now I have used #import "Wow_Dictionary-Swift.h", where Wow Dictionary is my project name. Now I am getting error Wow_Dictionary-Swift.h not found. Still something wrong?

@Bill 2017-04-22 13:36:52

@Aamir Actually, it's the target name, not the project name. Let me update my answer.

@Aamir 2017-04-22 13:37:59

@Bill my project name is same as target name.

@fnc12 2017-08-01 05:26:16

I did this but I get 10 compile errors saying that No type or protocol UIApplicationDelegate and similar

@John Ottenlips 2018-01-03 20:55:03

Do you have to import it in your .m file?

@Bill 2018-01-03 21:27:26

@JohnOttenlips yes

@lelelo 2018-01-29 19:31:58

This auto-generated file is now called TargetName-Bridging-Header.h though?

@Bill 2018-01-30 00:36:18

@lelelo No, that file is the opposite of this one. The bridging header is written by the developer and lets you bring Objective-C and C symbols into your Swift code. The Target-Swift.h file is automatically generated and lets your Objective-C/C code access Swift symbols.

@atulkhatri 2018-03-19 05:59:57

It's not necessarily the Target Name. It is actually Product Module Name which you have defined in Build Settings. You can have a look at the answer by Sumit Singh

@Adeel 2018-09-04 11:03:22

@Bill sorry to bother you but I have been trying to import the Swift header file but couldn't succeed. The issue is that I have 2 targets in my project. One of the Objective-C files requires to access the Swift header files of both the targets. How would I go about that?

@Bill 2018-09-04 11:05:19

Hi @Adeel You can create a new header file that #import's Target1-Swift.h and Target2-Swift.h and import that header into your Objective-C files.

@Adeel 2018-09-04 11:21:54

In the newly created header file it doesn't let me include either of the two. Moreover, if I exclude the objective-c file from one of the targets then the compiler stops complaining about the header file but starts complaining about one of objc class's subclass because that is included in both the targets.

@Itachi 2018-12-21 03:36:20

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!

@Johannes 2019-02-11 16:02:23

Important note: You can change the module name for each target to make the name of the swift header the same for each target. Details here -> stackoverflow.com/a/27411394/956816

@Sumit singh 2015-07-06 09:22:58

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"

@Zakaria Braksa 2015-12-31 13:35:29

Changing the value of Defines module in Build settings to Yes is what fixed things for me!

@Mohit 2016-02-22 11:05:30

Embedded Content Contains Swift : YES USE this also

@Sam B 2016-07-17 23:57:45

good answer but steps 7-8 are not needed in Xcode 7.3.1 and higher

@itsdamslife 2016-12-07 13:30:06

Worked for me! Thanks.

@Jaydeep Vyas 2017-07-27 07:40:35

I have skipped step NO. 6 even it is working for me... is this step really needed.??? my define module is set to NO

@jose920405 2017-09-05 18:27:49

Not works for me in xcode 8.3.2. Works in previous versions

@Sumit singh 2018-01-11 06:26:00

@jose920405 let me know what step you are following because the same steps for same Xcode version is working for me.

@Chuck Boris 2019-04-26 13:32:42

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.

@Ram G. 2018-06-18 20:31:18

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

@Misha Karpenko 2017-10-27 10:07:12

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.

@Misha Karpenko 2017-09-08 08:12:34

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

@Roi Mulia 2017-10-03 13:27:08

Better than the approved. The dashes wasted 20 min I'll never get back haha.

@Kiran Jasvanee 2017-07-04 10:59:42

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

@Nandkishor Chaudhari 2018-07-13 05:35:50

its working for me thanks

@Darshan Panchal 2017-05-17 09:26:06

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"

@Philipp Otto 2017-06-20 07:43:01

The forward declaration (@class) of the class is a very good point! Thanks!

@Dheeraj D 2016-12-07 06:32:00

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.

@rd_ 2017-09-09 07:10:39

i am not getting this option of use legacy swift language version

@Lukas Kalinski 2016-04-22 11:46:53

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.

@KingPolygon 2015-09-29 20:18:22

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

@Esha 2017-11-15 11:12:33

Thanks a lot! you saved my day.

@NSPratik 2018-11-17 10:52:39

This works like a charm if we installed swift-made library through Cocoapods. Upvoted !!!

@Andrei Papancea 2014-11-11 20:05:42

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

@cynistersix 2014-11-12 19:11:09

Worked perfectly

@Alphonse R. Dsouza 2015-08-13 12:17:25

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'?"

@AJit 2015-09-28 20:53:52

Not working with .mm file?

@Alejandro Iván 2016-01-14 17:05:51

@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/Conceptu‌​al/…

@David T. 2016-12-08 01:50:39

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

@inigo333 2017-04-03 10:19:54

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>

@Ricardo 2017-08-30 23:00:03

What if you want import a Swift extension?

@jose920405 2017-09-05 18:25:20

In my case "ProductName-Swift.h" works perfect in xcode 7.3 but now works in xcode 8.3.2

@Darshan Mothreja 2018-01-05 22:47:14

@AlejandroIván I have UIView swift class will it work ? I am not able to import swift into objective c

@Martin-Gilles Lavoie 2018-03-13 20:40:41

The missing module was my issue for a test project. Furthermore, if your project name starts with a numerical, (eg, 1234.xcodeproj), it too will replace the first offending character with _, thus your module name will default to "_234" and thus your include will look like "_234-Swift.h".

@Tamás Sengel 2018-11-08 14:39:29

"Note that you cannot subclass a Swift class in Objective-C." Thank you for including this, it saved me from going crazy.

@Nitin Alabur 2014-06-08 01:15:45

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"

@Viktor Radchenko 2014-06-08 01:32:12

Showed me this error: 'SCLAlertView-Swift.h' file not found.

@Bill 2014-06-08 04:02:18

Needs to be the project name, not the filename - see my answer.

@louielouie 2014-06-09 22:09:38

Thanks for the documentation link. That was useful for reference.

@Chris 2015-05-24 13:41:09

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

Related Questions

Sponsored Content

17 Answered Questions

[SOLVED] How do I call Objective-C code from Swift?

  • 2014-06-02 20:05:42
  • David Mulder
  • 267856 View
  • 919 Score
  • 17 Answer
  • Tags:   objective-c swift

24 Answered Questions

[SOLVED] Can't use Swift classes inside Objective-C

45 Answered Questions

[SOLVED] How do I import an SQL file using the command line in MySQL?

31 Answered Questions

[SOLVED] Swift to Objective-C header not created in Xcode 6

19 Answered Questions

[SOLVED] #pragma mark in Swift?

  • 2014-06-03 14:05:56
  • Arbitur
  • 209280 View
  • 899 Score
  • 19 Answer
  • Tags:   swift

57 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

15 Answered Questions

[SOLVED] Importing CommonCrypto in a Swift framework

13 Answered Questions

[SOLVED] Global constants file in Swift

  • 2014-10-08 08:20:15
  • user1028028
  • 146483 View
  • 314 Score
  • 13 Answer
  • Tags:   ios objective-c swift

9 Answered Questions

[SOLVED] When should I use curly braces for ES6 import?

18 Answered Questions

Sponsored Content