March 10, 2015

How do you integrate Swift code into Objective-C?

I have switched to Swift recently. As result, new code for Saver is mostly written using faster enums and structs, locally-scoped extensions, super-lazy vars, safer let and switch statements, meaningful square brackets etc. Only bug fixes and minor changes not worth a separate file are still coded in Objective-C.

But there is a problem. I cannot think up a better way to incorporate global .swift functions into .m classes other than creating a “fake” @objc class with a class func which takes only one argument and usually returns void.

@objc class CalculatorLayout {
    class func placeIntoViewController(viewController: UIViewController) {
        let view = viewController.view
        for cell in calculatorCells {
            let button = buttonWithCell(cell)
#import "Saver-Swift.h"
@implementation SVRCalculatorViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [CalculatorLayout placeIntoViewController:self];

This code definitely smells. I looked for possible solutions in Swift Documentation and on StackOverflow, but all tutorials and hints that I found recommend the right way only for “pure” object classes with regular methods.

So how do you deal with “value-typed” and other “non-OOP” Swift code in Objective-C projects?


  1. Great Information and such a great post! It is very informative and suggestible for the user of Objective-C I’ve been reading Apple’s documentation and I couldn’t figure out how to bring up the auto generated swift header file into objective-C code.

  2. Why would you need global functions? It's generally a bad idea if you have categories. As a side note I tried using Swift but didn't find it a competitive language if compared to Objective C.