Import Swift classes into an Objective-C structure

I have a custom Framework that has a bunch of Objective-C classes. Within the Framework, I would like to add additional classes using Swift. However, when you try to open Swift classes for Objective-C code using: MyProduct-Swift.h , it appears as " MyProduct-Swift.h file not found".

I tried this in one template and it works great. Is it not possible to import Swift into frames?

I also confirmed that I set the Defines Module parameter and the module name. I tried this with and without these settings.

+7
objective-c xcode swift
source share
2 answers

I found a few extra steps to make this all work. In addition to setting the “Define Module” to YES and the “Product Module Name” (usually they are set correctly by default if you create a new sensor structure in Xcode 6), you also need to:

  • add the 'public' keyword to all Swift classes you need in Objective-C
  • you need to add the keyword "public" even to all the methods and properties of the class you want to access from Objective-C
  • You need to import the Swift header in a long way:

#import <ProjectName/ModuleName-Swift.h>

+8
source share

Ok, I found that problem.

Make sure that in the Build → Packaging settings you have the Define Module set to YES and the name of the product module. Then, in the storyboard file, the name for the module should be the same and match what you have in the build settings. Then in your file in the Obj-C file, specify "-Swift.h"

This is a trick for me.

+4
source share

All Articles