0

I have a Swift project, and I successfully added some Objective C class files let say ObjectiveClass.h, into this Swift project and able to use it in the project, using bridging header. Now in the ObjectiveClass.h class, I want to import a Swift class of the Swift project. How can I do this?

I've searched a lot but not found any answers for this yet. Most answered questions are about how to import Objective-C class into Swift project, or import Swift class into Objective-C project. But my case is different

kenvu
  • 343
  • 6
  • 18

2 Answers2

0

Directly from the appledoc.

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h".

The thing most people don't get is that, the xyz-Swift.h header file will be generated by the xcode, you just need to believe that. At first i was searching the whole project source folder, where is this file, couldn't find it. You just need to import that and voila. Sometimes you will get error msg like xyz-Swift.h not found, clean your project, it will work smoothly.

Ratul Sharker
  • 7,484
  • 4
  • 35
  • 44
  • But my case is that I have Swift project, and I already successfully imported an Objective-C file into the Swift project using Bridging header. But I declare some common functions in Swift, and I want to use those functions in the Objective-C file, I don't know how to import the swift file into the .m Objective-C file – kenvu Nov 14 '16 at 13:39
  • 1
    read the answer & the doc carefully. – Ratul Sharker Nov 14 '16 at 13:42
0

Very Simple. Just declare @objc public before the swift class. Like this

@objc public class YourSwiftClass : NSObject {

}

Now in the objective c class use #import "YourProjectName-Swift.h". You will get error that the mentioned file was not found. Don't worry, just run the project once. When you click on the file at runtime you will see the contents of "YourProjectName-Swift.h" . Now you can access any methods of the swift class.

Poles
  • 3,585
  • 9
  • 43
  • 91