ios - property - xcode use objective c in swift

Can't use Swift classes inside Objective-C (14)

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

Details: Objective-C project with Swift 3 code in Xcode 8.1


  1. Use swift enum in objective-c class
  2. 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)


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,

    1. search for this text "brid" and set a path of your header file.
    2. "Defines Module": YES.
    3. "Always Embed Swift Standard Libraries" : YES.
    4. "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.

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.

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" 


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.

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.

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.

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 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 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:

  1. Create new *.swift file (in Xcode) or add it by using Finder
  2. Create an Objective-C bridging header when Xcode ask you about that
  3. Implement your Swift class:

    import Foundation
    // use @objc or @objcMembers annotation if necessary
    class Foo {
  4. 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
  5. Import Swift interface header in your *.m file

    #import "myproject-Swift.h"

    Don't pay attention to errors and warnings.

  6. Clean and rebuild your Xcode project
  7. Profit!

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.

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.

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.

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.

  1. in my case, I had space in my project name and xcode replace this is '_'