Using Swift in Objective-C projects

James Tang
iOS Apprentice
Published in
3 min readFeb 1, 2015

Apple releases Swift since WWDC 2014, a whole new language to replace Objective-C, and they did a lot of work to make the transition as seamless as possible. It’s so good that you can use both languages in the same project.

Now, lets see how easy is to use Swift in an existing Objective-C project:

1. Open up your existing Objective-C project

If you don’t have one, feel free to create a new one and follow on. We’ll name it “ObjectiveSpring” here.

2. Drag and Drop your .swift files into the project

If it ask do you wanted to add a bridging header, unless you plan to write Swift code to use your Objective-C classes, you can click NO for now.

If you don’t have a decent Swift framework, scroll to the bottom ;D

3. Using Swift classes in Interface Builder

Xcode automatically scans all .swift files you added in the project, so you can use a Swift UI component in Interface Builder without extra steps.

4. Using Swift classes in Objective-C implementation files (.m)

You may notice something like this if you tried to use a Swift class in code.

It’s because we actually have to tell Objective-C to import an “Umbrella” header to make it available for your class.

#import "YourProject-Swift.h"

In our case should be something like this:

So that’s it for mixing Swift in Objective-C project, pretty easy right?

Update: You can download ObjectiveSpring from my github now ☺

Recently we released our shiny new Spring animation framework, take advantages of IBInspectable, as well as optimized to use in code.

It also contains a number of interesting utilities classes, so please feel free to use and contribute, and let me know what you think!

--

--

James Tang
iOS Apprentice

Sketch Plugins and iOS UX Engineer. Opensource projects contributor, share on Twitter. @jamztang