---
title: "Mixing Swift And Objective C Code In The Same Project"
description: "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."
url: https://manytomany.co/blog/mixing-swift-and-objective-c-code-in-the-same-project/
published: 2015-04-22
---

# Mixing Swift And Objective C Code In The Same Project

22 April 2015 · Maxim Bilan

This is an archived article from the website of Factorial Complexity Ltd., an older incarnation of many-to-many. Information may be outdated or inaccurate.

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](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html). 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](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-1.png?0ab06fb561)

After that, you will see the following popup message:

![Bridging header file creation in Xcode for Swift-Objective-C integration](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-2.png?842afb67a4)

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:

    ```swift
    @import UIKit

    @objc class SwiftController: UIViewController { }
    ```

-   Defines module set to Yes:

    ![Code example showing Objective-C class import in Swift project](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-3.png?095d0bcb50)

-   Embedded Content Contains Swift set to Yes:

    ![Swift class declaration being used in Objective-C implementation](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-4.png?3f19f71fd3)

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

    ![Xcode build settings for mixed Swift and Objective-C projects](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-5.png?c4b4e4624b)

-   And please include header for Swift compatibility for example to pch file.

    ```objc
    #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](https://github.com/maximbilan/ios_swift_objc_mix).

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](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-8.png?4072f46f69)

## 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](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-9.png?ddb1f0da1d)

And also please choose Yes:

![Final project structure showing successful Swift and Objective-C integration](https://drw9cjciukmjl.cloudfront.net/blog/mixing-swift-and-objective-c-code/mixing-swift-obj-c-10.png?6b93a8b3d4)

Include our Obj C View Controller to bridging header:

```objc
#import "ObjCViewController.h"
```

And that’s all.

You also you can found example on [github](https://github.com/maximbilan/ios_swift_objc_mix).

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

![Vitalii](https://manytomany.co/vitalii-yellow.png)

## 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](https://calendly.com/many-to-many/intro) or reach me directy by email [hello@manytomany.co](mailto:hello@manytomany.co).

Or using your preferred messaging app:

 [![WhatsApp QR Code](https://manytomany.co/qr/qr-whatsapp.png)](https://wa.me/qr/QH6GQNCO5CZEO1)[WhatsApp](https://wa.me/qr/QH6GQNCO5CZEO1)

 [![Telegram QR Code](https://manytomany.co/qr/qr-telegram.png)](https://t.me/wicharek)[Telegram](https://t.me/wicharek)
