kenvu kenvu - 22 days ago 10
Objective-C Question

iOS Swift project, already imported Objective-C file and I want to import a swift class into this Objective-C

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

Answer

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.