How to use Swift code in Objective-C

Apple told you about it, but not very clean. The process is actually pretty simple.

Xcode generates a header file for all your Swift code in the project, so you can use it in Objective-C. Yes, if you have multiple Swift files in your project, you shouldn’t think about what file you need to import — it’s just one for all Swift code. The file name of this header is "ModuleName-Swift.h". And we’ll set it up now in a few steps.

1. Prepare your Swift code

In this step I’ll just repeat Apple’s words:

By default, the generated header contains interfaces for Swift declarations marked with the public modifier. It also contains those marked with the internal modifier if your app target has an Objective-C bridging header. Declarations marked with the private modifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with @IBAction@IBOutlet, or @objc as well.

— Apple.

So they can be classes, structs, protocols, enums, functions, variables and so on. Everything that can be marked with these modifiers: private, internal and public. A declaration that has no modifier is by default internal, so it’s contained in the generated header only if your app target has an Objective-C bridging header, as stated above.

Also, if your Swift class doesn’t inherit from NSObject class (or any other Objective-C class, like UIView), then you must mark this class with an objc keyword.

2. Check your project settings

As I said at the beginning of the article, the file name of the generated header is "ModuleName-Swift.h". The ModuleName here is the Product Module Name, let’s get to it:

1. Select your project in Project Navigator, then select project name under the word Project, so it’ll show you your project settings.

2. Select Build Settings tab on top, and find the Packaging section (you can type its name in the search bar).

Here’s Product Module Name. If its value is the name of your project, then let it be, but you may want to change it if the value is a little bit different from the name of the project. I’d recommend not to use spaces or any other non-alphanumeric characters, here’s why:

If it has spaces or any other non-alphanumeric characters, they’ll become replaced with underscores (_) in the header file name. Also, Apple tells us that “if the name begins with a number, the first number is replaced with an underscore”.

Alright, that’s it. Now build the project by pressing ⌘B or using the top menu ProjectBuild. Make sure that you have no build errors at all — Xcode must say “Build succeeded”. If it happened, then Xcode have probably generated the header. Remember: it’ll not appear in your project in Xcode or in its folder. It’s generated into a temporary build directory. Try to import it into the file where you want to use your Swift code by typing

#import "ModuleName-Swift.h"

In which ModuleName is your Product Module Name. You have to type it blindly, there’s no autocompletion here. So type this and build your project again. If you got “Build succeeded” message, then you’re done and Swift code is successfully integrated into Objective-C. It even autocompletes your class names, methods and variables written in Swift :)

If you have any problems after this, write them down in comments.

10 thoughts on “How to use Swift code in Objective-C”

  1. Thank you so much for putting together this blog. I read Apple’s documentation and I just couldn’t figure out how to bring up the auto generated swift header file into objective-C code. Thanks again!

  2. hi,
    i did exactly your steps.
    changed my Product Module Name,and added the private key to my swift class.
    but i still can’t import my swift class in the objective c class.
    any idea?

    thanx for advance!

  3. Hi,
    i did exactly the steps and the swift stuff could successfully used in AppDelegate.m.
    However, any other .m files in my objective-c project cannot use the swift class i created;
    when i tried to import the header, it said that the header is not found.
    Why I successfully import the header in AppDelegate.m while it cannot be imported in other files?

    Thank you in advance.

  4. Hello,

    I did it in the same way like you did but When I import it inside the .m file, build succeeded but when I want to import it inside .h file then it show an error : ‘module_name-Swift.h’ file not found.

    thank you in advance.

Leave a Reply.