Boosa Ramesh
Boosa Ramesh

Reputation: 389

Import Objective-C class in Swift file in Objective-C Project

I'm currently working with Objective-C project, and i'm using some swift files in that so created bridge header like APPNAME-Swift.h. and i'm able to import in objective-C classes. But here my problem is i need to import objective-c class in swift file.

here is example:

Objective-C project>drag and drop swift files ClassA.swift, ClassB.swift into project and created bridge. Now in objective-C class i'm able to use swift classes in objective-c classes like below Appdelegate.h i imported #import "<#YourProjectName#>-Swift.h"

now i need to import Appdelegate.h in ClassB.swift.

Please Help me on it

Upvotes: 1

Views: 3361

Answers (1)

Roman Podymov
Roman Podymov

Reputation: 4531

You don't need to create the file "APPNAME-Swift.h" in your project, it will be generated automatically during build and you should not try to modify this file. You can create bridging header for your project but I guess You don't have a problem with it. I just recommend you to rename your bridging header, because I guess that the generated file "APPNAME-Swift.h" has the same name as your bridging header, that's why you have a problem.

After you will rename your bridging header change your build settings. You will need to change a string at Build Settings -> Objective-C Bridging Header (see attached image for more information):

Setup of bridging headers

If you want to change name of the file "APPNAME-Swift.h" you need also modify your build settings. You can do it at Build Settings -> Objective C Generated Interface Header Name (see attached image for more information):

Setup of the Objective C interface file

In your bridging header add the following line:

#import "MyObjectiveCClass.h"

And after that you will be able to use class MyObjectiveCClass (Objective-C) in your Swift code (if file "MyObjectiveCClass.h" contains a class called "MyObjectiveCClass").

All settings provided for Xcode 9.2.

Upvotes: 2

Related Questions