2

I found a ViewController SDK using objective C. I want to use it swift but fail to call.

Can you suggest me any idea for using it?

I want to create a storyboard and set the custom class in ViewControllerObjectiveC, but still fail to open the viewcontroller.

let vc = sb.instantiateViewController(withIdentifier: "ViewControllerObjectiveC") as! ViewControllerObjectiveC
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.present(vc, animated: false, completion: nil)
Revanth Kausikan
  • 673
  • 1
  • 9
  • 21
ebank dsb
  • 21
  • 2
  • 8
    Possible duplicate of [How to call Objective-C code from Swift](https://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift) – El Tomato Mar 28 '19 at 02:53
  • Try to change last two lines as => self.dismiss(animated: true) { self.presentingViewController?.present(vc, animated: true, completion: nil) } – Pranay Mar 28 '19 at 05:23

2 Answers2

1

In order to interact Obj-C code with Swift you will need to have a bridging header file. The name of the bridging header MUST be like <YourProject>-Bridging-Header.h. While you can create it manually there is an option which the Xcode creates it yourself. For the second option create a new file with .m extension. The Xcode will ask you to create a bridging header. Approve it.

Now import everything in this file you need to reach from Swift. In your case put the following inside your bridging header:

#import "ViewControllerObjectiveC.h"

Assuming your Obj-C file has the name ViewControllerObjectiveC.h. The code you shared should work now.

Onur Tuna
  • 1,030
  • 1
  • 10
  • 28
0

Bridging Header is the way to solve your problem.

See this Apple Document:

https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift

Step 1 Drag and drop your Objective-C files into the project after that you will see this message inside your Xcode project.

See this popup

Then click on Create Bridging header option. It will create one header file inside your project.

Confirm Header File

Now you just have to Import your Objective C File into created header file.

That's It.