Generic Swift File header name used to import to Objective-C for multiple projects [duplicate] - objective-c

I try to integrate Swift code in my app.My app is written in Objective-C and I added a Swift class. I've done everything described here. But my problem is that Xcode haven't created the -Swift.h file, only the bridging headers. So I created it, but it's actually empty.
I can use all my ObjC classes in Swift, but I can't do it vice versa. I marked my swift class with #objc but it didn't help. What can I do now?
EDIT: Apple says:" When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. [...] The name of this header is your product module name followed by adding “-Swift.h”. "
Now when I want to import that File, it gives an error:
#import "myProjectModule-Swift.h" //Error: 'myProjectModule-Swift.h' file not found
#implementation MainMenu
Here is my FBManager.swift file:
#objc class FBManager: NSObject {
var descr = "FBManager class"
init() {
func desc(){
func getSharedGameState() -> GameState{
return GameState.sharedGameState() //OK! GameState is written in Objective-C and no error here

I spent about 4 hours trying to enable Swift in my Xcode Objective-C based project. My "myproject-Swift.h" file was created successfully, but my Xcode didn't see my Swift-classes. So, I decided to create a new Xcode Objc-based project and finally I found the right answer! Hope this post will help someone :-)
Step by step Swift integration for Xcode Objc-based project:
Create new *.swift file (in Xcode) or add it by using Finder
Create an Objective-C bridging header when Xcode ask you about that
Implement your Swift class:
import Foundation
// use #objc or #objcMembers annotation if necessary
class Foo {
Open Build Settings and check those parameters:
Defines Module : YES
Copy & Paste parameter name in a search bar
Product Module Name : myproject
Make sure that your Product Module Name doesn't contain any special characters
Install Objective-C Compatibility Header : YES
Once you've added *.swift file to the project this property will appear in Build Settings
Objective-C Generated Interface Header : myproject-Swift.h
This header is auto generated by Xcode
Objective-C Bridging Header : $(SRCROOT)/myproject-Bridging-Header.h
Import Swift interface header in your *.m file
#import "myproject-Swift.h"
Don't pay attention to errors and warnings.
Clean and rebuild your Xcode project

Don't create the header file yourself. Delete the one you created.
Make sure your Swift classes are tagged with #objc or inherit from a class that derives (directly or indirectly) from NSObject.
Xcode won't generate the file if you have any compiler errors in your project - make sure your project builds cleanly.

Allow Xcode to do its work, do not add/create Swift header manually. Just add #objc before your Swift class ex.
#objc class YourSwiftClassName: UIViewController
In your project setting search for below flags and change it to YES (Both Project and Target)
Defines Module : YES
Always Embed Swift Standard Libraries : YES
Install Objective-C Compatibility Header : YES
Then clean the project and build once, after build succeed (it should probably) import below header file in your objective-c class .m file
#import "YourProjectName-Swift.h"

Also probably helpful for those of you with a Framework target:
The import statement of the auto-generated header file looks a bit different from app targets. In addition to the other things mentioned in other answers use
#import <ProductName/ProductModuleName-Swift.h>
instead of
#import "ProductModuleName-Swift.h"
as per Apples documentation on Mix & Match for framework targets.

Make sure your project defines a module and you have given a name to the module. Then rebuild, and Xcode will create the -Swift.h header file and you will be able to import.
You can set module definition and module name in your project settings.

Details: Objective-C project with Swift 3 code in Xcode 8.1
Use swift enum in objective-c class
Use objective-c enum in swift class
1. Objective-C class which use Swift enum
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, ObjcEnum) {
#interface ObjcClass : NSObject
+ (void) PrintEnumValues;
#import "ObjcClass.h"
#import "SwiftCode.h"
#implementation ObjcClass
+ (void) PrintEnumValues {
[self PrintEnumValue:SwiftEnumValue1];
[self PrintEnumValue:SwiftEnumValue2];
[self PrintEnumValue:SwiftEnumValue3];
+ (void) PrintEnumValue:(SwiftEnum) value {
switch (value) {
case SwiftEnumValue1:
NSLog(#"-- SwiftEnum: SwiftEnumValue1");
case SwiftEnumValue2:
case SwiftEnumValue3:
NSLog(#"-- SwiftEnum: long value = %ld", (long)value);
Detect Swift code in Objective-C code
In my sample I use SwiftCode.h to detect Swift code in Objective-C. This file generate automatically (I did not create a physical copy of this header file in a project), and you can only set name of this file:
If the compiler can not find your header file Swift code, try to compile the project.
2. Swift class which use Objective-C enum
import Foundation
enum SwiftEnum: Int {
case Value1, Value2, Value3
class SwiftClass: NSObject {
class func PrintEnumValues() {
class func PrintEnumValue(value: ObjcEnum) {
switch value {
case .Value1, .Value2:
NSLog("-- ObjcEnum: int value = \(value.rawValue)")
case .Value3:
NSLog("-- ObjcEnum: Value3")
Detect Objective-C code in Swift code
You need to create bridging header file. When you add Swift file in Objective-C project, or Objective-C file in swift project Xcode will suggest you to create bridging header.
You can change bridging header file name here:
#import "ObjcClass.h"
#import "SwiftCode.h"
[ObjcClass PrintEnumValues];
[SwiftClass PrintEnumValues];
[SwiftClass PrintEnumValue:ObjcEnumValue3];
Full integration steps Objective-c and Swift described above. Now I will write some other code examples.
3. Call Swift class from Objective-c code
Swift class
import Foundation
class SwiftClass:NSObject {
private var _stringValue: String
var stringValue: String {
get {
print("SwiftClass get stringValue")
return _stringValue
set {
print("SwiftClass set stringValue = \(newValue)")
_stringValue = newValue
init (stringValue: String) {
print("SwiftClass init(String)")
_stringValue = stringValue
func printValue() {
print("SwiftClass printValue()")
print("stringValue = \(_stringValue)")
Objective-C code (calling code)
SwiftClass *obj = [[SwiftClass alloc] initWithStringValue: #"Hello World!"];
[obj printValue];
NSString * str = obj.stringValue;
obj.stringValue = #"HeLLo wOrLd!!!";
4. Call Objective-c class from Swift code
Objective-C class (ObjcClass.h)
#import <Foundation/Foundation.h>
#interface ObjcClass : NSObject
#property NSString* stringValue;
- (instancetype) initWithStringValue:(NSString*)stringValue;
- (void) printValue;
#import "ObjcClass.h"
#interface ObjcClass()
#property NSString* strValue;
#implementation ObjcClass
- (instancetype) initWithStringValue:(NSString*)stringValue {
NSLog(#"ObjcClass initWithStringValue");
_strValue = stringValue;
return self;
- (void) printValue {
NSLog(#"ObjcClass printValue");
NSLog(#"stringValue = %#", _strValue);
- (NSString*) stringValue {
NSLog(#"ObjcClass get stringValue");
return _strValue;
- (void) setStringValue:(NSString*)newValue {
NSLog(#"ObjcClass set stringValue = %#", newValue);
_strValue = newValue;
Swift code (calling code)
if let obj = ObjcClass(stringValue: "Hello World!") {
let str = obj.stringValue;
obj.stringValue = "HeLLo wOrLd!!!";
5. Use Swift extension in Objective-c code
Swift extension
extension UIView {
static func swiftExtensionFunc() {
NSLog("UIView swiftExtensionFunc")
Objective-C code (calling code)
[UIView swiftExtensionFunc];
6. Use Objective-c extension in swift code
Objective-C extension (UIViewExtension.h)
#import <UIKit/UIKit.h>
#interface UIView (ObjcAdditions)
+ (void)objcExtensionFunc;
#implementation UIView (ObjcAdditions)
+ (void)objcExtensionFunc {
NSLog(#"UIView objcExtensionFunc");
Swift code (calling code)

I had the same issue and it turned out special symbols in the module name are replaced by xcode (in my case dashes ended up being underscores). In project settings check "module name" to find the module name for your project. After that either use ModuleName-Swift.h or rename the module in settings.

The file is created automatically (talking about Xcode 6.3.2 here). But you won't see it, since it's in your Derived Data folder. After marking your swift class with #objc, compile, then search for Swift.h in your Derived Data folder. You should find the Swift header there.
I had the problem, that Xcode renamed my my-Project-Swift.h to my_Project-Swift.h Xcode doesn't like
"." "-" etc. symbols. With the method above you can find the filename and import it to a Objective-C class.

Just include
#import "myProject-Swift.h" in .m or .h file
P.S You will not find "myProject-Swift.h" in file inspector it's hidden. But it is generated by app automatically.

#sig answer is one of the best, however, it did not work for me with the old project (not new!), I needed some modifications. After a lot of variations I found the recipe for me (using XCode 7.2):
Product Module Name : $(PRODUCT_NAME:c99extidentifier)
Defines Module : NO
Embedded Content Contains Swift : NO
Install Objective-C Compatibility Header : YES
Objective-C Bridging Header : ProjectName-Bridging-Header.h
The last point (5) was crucial. I put it only on the second section (Targets field), the Project field should be left empty: Otherwise, it did not generate the right "Project-Swift.h" file for me (it did not include swift methods).

There is two condition,
Use your swift file in objective c file.
Use your objective c file in swift file.
So, For that purpose, you have to follow this steps:
Add your swift file in an objective-c project or vice-versa.
Create header(.h) file.
Go to Build Settings and perform below steps with search,
search for this text "brid" and set a path of your header file.
"Defines Module": YES.
"Always Embed Swift Standard Libraries" : YES.
"Install Objective-C Compatibility Header" : YES.
After that, clean and rebuild your project.
Use your swift file in objective c file.
In that case,First write "#objc" before your class in swift file.
After that ,In your objective c file, write this,
#import "YourProjectName-Swift.h"
Use your objective c file in swift file.
In that case, In your header file, write this,
#import "YourObjective-c_FileName.h"
I hope this will help you.

In my case, apart from these steps:
Product Module Name : myproject
Defines Module : YES
Embedded Content Contains Swift : YES
Install Objective-C Compatibility Header : YES
Objective-C Bridging Header : $(SRCROOT)/Sources/SwiftBridging.h
I have needed to put the class as public in order to create productName-Swift.h file:
import UIKit
#objc public class TestSwift: NSObject {
func sayHello() {
print("Hi there!")

I just discovered that adding a directory of swift files to a project won't work. You need to create a group first for the directory, then add the swift files...

I had the same problem and finally it appeared that they weren't attached to the same targets.
The ObjC class is attached to Target1 and Target2, the Swift class is only attached to the Target1 and is not visible inside the ObjC class.
Hope this helps someone.

I have the same error: myProjectModule-Swift.h file not found", but, in my case, real reason was in wrong deployment target:
"Swift is unavailable on OS X earlier than 10.9; please set MACOSX_DEPLOYMENT_TARGET to 10.9 or later (currently it is '10.7')"
so, when I've changed deployment target to 10.9 - project had been compiled successfully.

My issue was that the auto-generation of the -swift.h file was not able to understand a subclass of CustomDebugStringConvertible. I changed class to be a subclass of NSObject instead. After that, the -swift.h file now included the class properly.

my problem was I got stuck after xcode created the bridge file but still I got error in header file name MYPROJECTNAME-swift.h
1.I check in terminal and search for all auto created swift bridge files:
find ~/library/Developer/Xcode/DerivedData/ -name "*-Swift.h"|xargs basename|sort -
you see what xcode created.
in my case, I had space in my project name and xcode replace this is '_'

I had issues in that I would add classes to my objective-c bridging header, and in those objective-c headers that were imported, they were trying to import the swift header. It didn't like that.
So in all my objective-c classes that use swift, but are also bridged, the key was to make sure that you use forward class declarations in the headers, then import the "*-Swift.h" file in the .m file.

I didnt have to change any settings in the build or add #obj to the class.
All I had to do was to create bridge-header which was automatically created when I created Swift classes into Objective-c project. And then I just had to do
import "Bedtime-Swift.h" <- inside objective-c file that needed to use that swift file.

well, after reading all the comments and trying and reading and trying again, I managed to include swift classes into my Big obj-c project.
So, thanks for all the help.
I wanted to share one tip that helped me understand the process better.
In the .m class, went to the import line of the swift target name #import "myTargetName-Swift.h"
and clicked the key:
command + mouse click -> Jump to definition
There you can see all the translation from swift to obj-c and ther you will find the various functions re-declared in obj-c.
Hope this tip will help you as much as it helped me.

After doing everything above, I still got errors. My problem ended up being that the Swift files I needed weren't added to the bundle resources for some reason.
I fixed this by going to [MyTarget] > Build Phases > Copy Bundle Resources, then clicked the plus button and added the Swift files.


Unable to call swift function in objective C. Getting error interface decleration for NSObject

Am getting error while calling swift function in objective c file.
I have created "-Swift.h" file.
I have created ".swift" file. My code here is
import Foundation
#objc class className : NSObject {
#objc func someFunc(sender: AnyObject) {
//Some code
I have created "-Swift.h" header file. My code here is
#ifndef ProjectName_Swift_h
#define ProjectName_Swift_h
#endif /* ProjectName_Swift_h */
#class className;
#interface className : NSObject
In my objective C ".m" file am calling the swift function.
#import "XXX-Swift.h"
- (void)viewDidLoad
[super viewDidLoad];
className *obj = [[className alloc]init];
[obj someFunc];
Now am getting error in "XXX-Swift.h" file.
ERROR: Cannot find interface declaration for 'NSObject', super class for 'className'.
delete the file you created ending in -Swift.h, that file is auto-generated by Xcode.
you can view the Xcode generated version (after a successful build) if you command click #import "XXX-Swift.h"
if you go into your build settings and search for SWIFT_OBJC_INTERFACE_HEADER_NAME you can see the file name.
you have to created Bridging header file and import you swift file there.
Use this link, it will help you
I found difference between manually creating -Bridging-header file and automatically Xcode generated -Bridging-header file. I have deleted the -Swift.h file and manually created -Bridging-header file. Added -Bridging-header(Xcode generated one) file. It automatically created -Swift.h file after successful build of project.

How can I use method of an ObjC class which contains a forward declare Swift class in Swift?

class CoolSwiftClass: NSObject {}
#class MyCoolSwiftClass;
#interface MyObjCViewController : UIViewController
- (instancetype)initWithMyCoolSwiftClass:(MyCoolSwiftClass *)myCoolSwiftClassInstance;
class SadSwiftClass: UIViewController {
override func loadView() {
//This won't work
let myObjCViewController = MyObjCViewController(coolSwiftClass: coolSwiftClassInstance)
It seems that Swift can find the forward declaration MyCoolSwiftClass but it cannot find out that it's actually the CoolSwiftClass.
And I didn't find anything that can help me in Swift and Objective-C in the Same Project.
Update at 2015-11-19 13:34 CST
Just upload a repo to GitHub: SO33775908
Update at 2015-11-19 13:47 CST
Just find out a workaround:
No special ObjC name. Checkout branch No special ObjC name
Update at 2015-11-19 14:47 CST
Another workaround which generate warnings:
#compatibility_alias with several warnings
The best way I ever found is the workaround 1:
No special ObjC name
And if you have used that special ObjC name for a while, you can use something like this:
#define MyCoolSwiftClass CoolSwiftClass
class CoolSwiftClass: NSObject {}
Please checkout Question Update 1 for the sample repo.

Can objective-c code call swift extension on Class?

I searched some posts, I think I cannot write an extension under swift, and call it from Objective-C code, right ?
#objc like attributes only support methods, class, protocols ?
You can write a Swift extension and use it in ObjectiveC code. Tested with XCode 6.1.1.
All you need to do is:
create your extension in Swift (no #objc annotation)
import "ProjectTarget-Swift.h" in your ObjectiveC class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)
call the methods from the Swift extension
I found out that in Swift 4.0 I had to add #objc in front of my extension keyword in order for the Swift extension methods to become visible by an instance of the Objc class I was extending.
In short:
File configuration setup:
In CustomClassExtension:
#objc extension CustomClass
func method1()
In my AppDelegate.m:
self.customClass = [[CustomClass alloc] init];
[self.customClass method1];
This solution works for Swift 2.2 and Swift 3. Note that only extensions for classes (not for structs or enums) will be accessible from Objective-C.
import UIKit
extension UIColor {
//Custom colours
class func otheEventColor() -> UIColor {
return UIColor(red:0.525, green:0.49, blue:0.929, alpha:1)
Then #import "ProductModuleName-Swift.h" in your ObjC file.
As covered in the other answers, importing the generated Swift header works in most cases.
An exception to this is when the category is defined on a bridged type (i.e. the extension is defined on String and not NSString). These categories will not automatically be bridged to their Objective-C counterparts. To get around this, you'll either need to use the Objective-C type (and cast the return value in your Swift code with as String) or define an extension for both the Swift and Objective-C types.

Swift protocol in Objective-C class

I wrote SearcherProtocol in Swift and need to implement an Objective-C class FileSearcher which has to use this protocol.
So I tried this:
#import <Foundation/Foundation.h>
#interface FileSearcher : NSObject <SearcherProtocol>
// ... class content
The compiler tells me
Cannot find protocol declaration for 'SearcherProtocol'
The corresponding bridged header file (modulename-Swift.h) is being imported within FileSearcher.m.
Importing SearcherProtocol into FileSearcher.h throws another compiler error: module name-swift.h file not found
Does anybody have any clue what I'm doing wrong?
I'm using Xcode 6 Beta 5.
Here is the protocol declaration in Swift:
#objc protocol SearcherProtocol
var searchNotificationTarget: SearchCompletedProtocol? { get }
var lastSearchResults: [AnyObject] { get set }
func search(searchParam: String, error: NSErrorPointer) -> Bool
And the SearchCompletedProtocol:
#objc protocol SearchCompletedProtocol
func searchCompletedNotification(sender: AnyObject!)
There are two common reasons for this occuring:
Getting the module name wrong, see my answer.
Having a circular reference - see mitrenegades answer below.
1. Get the module name right:
If both the swift protocol and and Objective C are in the same project then according to apple you should just need to make sure you get the correct module name.
For Xcode6 beta 5 you can find it under BuildSettings->Packaging->Product Module Name
A common mistake would be to think that each swift file/class gets its own file, but instead they are all put into one big one that is the name of the project.
A further mistakes are if the module name has spaces, these should be replaced with underscores.
With your protocol I created a test project called 'Test' which compiles perfectly and it has the files:
#import <Foundation/Foundation.h>
#import "Test-Swift.h"
#interface TestObjCClass : NSObject <SearcherProtocol>
#import "TestObjCClass.h"
#implementation TestObjCClass
import Foundation
#objc protocol SearcherProtocol
var searchNotificationTarget: SearchCompletedProtocol? { get }
var lastSearchResults: [AnyObject] { get set }
func search(searchParam: String, error: NSErrorPointer) -> Bool
#objc protocol SearchCompletedProtocol
func searchCompletedNotification(sender: AnyObject!)
2. Avoid circular reference:
Mitrenegades answer explains this, but if your project needs to use the explicit objc class that uses the swift protocol, (rather than just using the protocol) then you will have circularity issues. The reason is that the swift protocol is defined to the swift-objc header, then to your obj-c class definition, which then goes again to the swift-objc header.
Mitrenegades solution is to use an objective-c protocol, is one way, but if you want a swift protocol, then the other would be to refactor the code so as to not use the objective-c class directly, but instead use the protocol (e.g. some protocol based factory pattern). Either way may be appropriate for your purposes.
When you have
#import "moduleName-Swift.h"
in the .h file that you want to be a delegate, and you have that .h file also in the bridging headers file, there's a circular reference that causes the moduleName-Swift.h to fail compilation. for #james_alvarez's test project, it's probably working because you don't need to include TestObjClass.h into the bridging header.
The best way for me to combine objc files that need to be the delegate for a class written in swift, but that also needs to be included in the bridging header so other swift files can access this objc class, is to create a separate protocol file in objc:
#protocol MyDelegate <NSObject>
#import "MyProtocol.h"
#interface ViewController : UIViewController <MyDelegate>
#import "MyProtocol.h"
#import "ViewController.h"
I know this was a long time ago, but I just struggled with the same problem when adding a protocol to my Swift code, and it wasn't being added to the -Swift.h header file, hence "Cannot find protocol declaration"
The problem was my protocol wasn't marked as Public. I changed my protocol from this:
#objc protocol MyProtocol { //etc.... }
to this:
#objc public protocol MyProtocol { //etc.... }
I'm still not entirely sure why I need 'Public' but nobody else seems to, but hey it works...
Make sure you are including the auto generated Swift header in your ObjectiveC file. It will have the same name as your project module followed by -Swift.h.
For instance if your Project Module is MyTarget, then you would use:
#import "MyTarget-Swift.h"
If you are typing in the import into your Objective C file, it will not autocomplete. You can verify that you have the correct file by Command-clicking on the header after typing it in.
Import delegate as like this in .h file
#protocol AnalyticProtocol;
and add this in to .swift file
#objc public protocol AnalyticProtocol {
Try adding #import "Product_Module_Name-Swift.h" to your Product_Module_Name-Prefix.pch file. That fixed it for me, plus you will now have access to your swift files from any objc file.

Call Swift function from Objective C class

I have a problem. I have an Old Objective C project and i want to call new Swift function and object, i have create the file "-Bridging-Header.h" and "-Swift.h"
was easy for me call the function from Swift to Objective C but i have a problem for reverse.
So i have create a simple class "System.Swift"
import Foundation
#objc class System : NSObject {
#objc func printSome() {
println("Print line System");
now i have try to follow the documentation here and inside the <...>-Swift.h file i have write this
#class System;
#interface System : NSObject
and i have import it inside my Objective C Class. At this point inside my Objective C class (currently UIViewController) of my Objective C Code i have try to call "printSome" method:
- (void)viewDidLoad
[super viewDidLoad];
System * sis = [[System alloc] init];
[sis printSome];
//any additional setup after loading the view from its nib.
now i have the follow Error:
Undefined symbols for architecture i386: "_OBJC_CLASS_$_System",
referenced from:
objc-class-ref in "ObjectiveC_Class_That_Call_Swift_Object".o ld: symbol(s) not found for architecture i386 clang: error: linker
command failed with exit code 1 (use -v to see invocation)
Some one can help me?
Regards all.
Problem Solved, I previously create and included a new .h file in my Objective-C class named <ProductModuleName>-Swift.h but, as i discovered later, this step is not necessary because the compiler creates the necessary file invisible.
Simply include <ProductModuleName>-Swift.h in your class and it should work.
Yes Thanks
it is strange but it'll work
1) Add #objc to Swift class
2) Add in .m
#import "(ProjectName)-Swift.h"
3) Call from .h
#class SwiftClass;
4)On SwiftClass
click "Command" + Left Click (Apple Documantation)
5) To see "-Swift.h" -> click "Command" + Left Click
App will generate the interface for this class in -Swift.h
#interface PLHelper
(void)notifyForDownloading:(NSDictionary *)userInfo;
Assume We have ProjectName "MyFirstProjectOnSwift" and swift class name "mySwiftClass" and objectiveC class is "MyObjectiveCLass"
Following steps are:-
Add #import "MyFirstProjectOnSwift-Swift.h" in "MyObjectiveCLass.m"
Add #class mySwiftClass in MyObjectiveCLass.h;
Then in MyObjectiveCLass.m
mySwiftClass *myClass = [mySwiftClass new]; {Call Like This in any
method wherever you want to call swift method.}
[myClass methodName];
Check the -Swift.h file which has the import on your .m file in objective-C:
#import <YourProjectName-Swift.h>
Click on that line, and left-click - Jump To Definition.
This file should be included automatically, not manually.
If you're still not seeing your class even after importing <ProductModuleName>-Swift.h.
Make sure that your class is subclassing a native class (e.g UIViewController) or at least if it's just a utility class, make it subclass NSObject. Your class should now show.