Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/maximbilan/ios-swift-objc-mix

An example of the mixing Swift and Obj C code in the same project
https://github.com/maximbilan/ios-swift-objc-mix

ios objective-c swift xcode

Last synced: about 3 hours ago
JSON representation

An example of the mixing Swift and Obj C code in the same project

Awesome Lists containing this project

README

        

An example of the mixing Swift and Obj 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 found here. But in spite of it I would like to share my experience on real examples, because it’s not so simple as it says.

## Swift code in Obj C project

Let’s start from Obj 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:

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/1.png)




After that, you will see the following popup message:


![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/2.png)




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.



1. Implement you Swift class with @objc attribute:


//
// SwiftController.swift
// ios_objc_mix
//
// Created by Maxim Bilan on 1/17/15.
// Copyright (c) 2015 Maxim Bilan. All rights reserved.
//

import UIKit

@objc class SwiftController: UIViewController {

}




2. Defines module set to ‘YES’:

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/4.png)


3. Embedded Content Contains Swift set to ‘YES’:

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/5.png)


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

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/6.png)


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


#import “Product Module Name-Swift.h”


//
// ios_objc_mix-PrefixHeader.pch
// ios_objc_mix
//
// Created by Maxim Bilan on 1/17/15.
// Copyright (c) 2015 Maxim Bilan. All rights reserved.
//

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

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/8.png)

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/9.png)


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 set up the module.

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/10.png)


## Obj C code in Swift project

It’s a similar process. And simpler.

Add Obj C file to project.

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/11.png)

And also please choose ‘YES’:

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/12.png)

Include our Obj C View Controller to bridging header:


//
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#import "ObjCViewController.h"


And that’s all.

You also you can find example on github.

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/14.png)

![alt tag](https://raw.github.com/maximbilan/ios_swift_objc_mix/master/img/15.png)


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