Skip to content

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

License

Notifications You must be signed in to change notification settings

maximbilan/iOS-Swift-ObjC-Mix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

After that, you will see the following popup message:
alt tag

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
alt tag

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

Obj C code in Swift project

It’s a similar process. And simpler.

Add Obj C file to project.
alt tag
And also please choose ‘YES’:
alt tag
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
alt tag

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published