Mixing Swift And Objective C Code In The Same Project

Yes, there is opportunity to combine this languages in the same project. It’s great. Apple provides a tutorial how to do this magic. You can find here. But in spite of it I would like to share my experience on real examples, because it’s not as simple as it sounds.

Swift code in Objective C project

Let’s start from Objective C project, and will try to add Swift code to this project. Create the Obj C project. And create new Swift class.

For example, UIViewController:

Xcode project settings showing Swift and Objective-C configuration

After that, you will see the following popup message:

Bridging header file creation in Xcode for Swift-Objective-C integration

Please, choose Yes.

Now, you have ios_objc_mix-Bridging-Header.h file. In this header you can import source files for your Swift class.

After that, you can do the next steps.

  • Implement you Swift class with @objc attribute:
    @import UIKit
    
    @objc class SwiftController: UIViewController { }
  • Defines module set to Yes:

    Code example showing Objective-C class import in Swift project

  • Embedded Content Contains Swift set to Yes:

    Swift class declaration being used in Objective-C implementation

  • After that you should found the Product Module Name in your target settings:

    Xcode build settings for mixed Swift and Objective-C projects

  • And please include header for Swift compatibility for example to pch file.
    #import “Product Module Name-Swift.h”
    
    #ifdef __OBJC__
        #import <UIKit/UIKit.h>
        #import <Foundation/Foundation.h>
    #endif
    
    #import "ios_objc_mix-Swift.h"

And now you can use Swift code. Please, see the example on github.

Note: ios_objc_mix-Swift.h file you can’t find in your project browser Xcode automatically generates this header. Magic!

Note: if you have a lot of targets in your project. It really is a pain. You should import magic Product-module-name-Swift.h headers for each target.

Note: if you use Swift classes in the Interface Builder, you should setup the module.

Error handling and debugging in mixed language iOS projects

Obj C code in Swift project

It’s similar process. And simpler.

Add Obj C file to project.

Interface file example for Swift-Objective-C interoperability

And also please choose Yes:

Final project structure showing successful Swift and Objective-C integration

Include our Obj C View Controller to bridging header:

#import "ObjCViewController.h"

And that’s all.

You also you can found example on github.

Hopefully this will save time for someone. Don’t forget star the github repository.

22 April 2015
Written by Maxim Bilan
Vitalii

Hi. I'm Vitalii.

I’m a software developer, technical manager and solo entrepreneur. I have been building software products all my life. I am offering CTO as a Service at many-to-many.

Contact me

Let me know what are you looking for. I will reach out to you via the provided email with quote and follow up questions. Alternatively, book a call or reach me directy by email hello@manytomany.co.

Sending...
error
We have received your message and will respond shortly. Thank you!

Or using your preferred messaging app: