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
publicmodifier. It also contains those marked with the
internalmodifier if your app target has an Objective-C bridging header. Declarations marked with the
privatemodifier do not appear in the generated header. Private declarations are not exposed to Objective-C unless they are explicitly marked with
So they can be classes, structs, protocols, enums, functions, variables and so on. Everything that can be marked with these modifiers:
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
2. Check your project settings
As I said at the beginning of the article, the file name of the generated header is
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 Project — Build. 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
<br /> #import "ModuleName-Swift.h"<br />
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.