

But wait, we have a huge problem now.Īdding public in front of our Swift classes and methods we are making them, you guess, public. Now we can start using Swift inside Objective-C code, yay ?. They also have to be marked with before the class name and they have to inherit from NSObject.

The generated header file is part of the framework's public interface and only Swift classes which are marked as “Public” or “Open” will be accessible from Objective-C code. To use Swift, we need to make some adjustments to our code. Step 3 – How to use Swift code inside ObjC Now that we are all set up, we can import our Swift file into Objective-C like this: #import Īnd you can start using Swift if you are lucky. Next, go to “Build Settings” of your framework project and locate “Defines Module” and set it to “YES” This is important since we will be using it to import Swift code into Objective-C. This generated interface header should be named as follows: “YourSDK-Swift.h”. If you don’t have a bridging header, it’s very simple to create one manually.Ĭreate a new Swift file and the popup will appear like on the image above, just click on “Create Bridging Header”. This is generated automatically by Xcode when you create or insert Swift files into your project. To import Swift code in Objective-C first of all you need to have an Objective-C bridging header. This is recommended by Apple and you can read more details about it on this documentation page. Change enum to NS_ENUM, this will improve code competition in Xcode and will specify the type and size of your enums.When creating properties you get by default getter and setter methods automatically, you will also have access to property attributes such as: weak, assign, retain etc.

