1

I am rewriting my app which was in Objective-C to Swift. I was wondering if I can use some old UIViewControllers from my previous app in my new application without having to rewrite these in Swift. Is it possible?

1
  • You should be able to mix them. They are UIViewController after all, it's the CocoaTouch framework, it's not dependent of the language used. Commented Jun 16, 2016 at 13:10

2 Answers 2

1

Yes you can use a bridging header.

When you import Objective-C files into a swift project (let's call the project MyApp), it should ask if you want to create the bridging header MyApp-Bridging-Header.h. If it doesn't ask, you can always create it yourself, but if you do it this way make sure you include it under the Swift Compiler - Code Generator -> Objective-C Bridging Header in the Build Settings of your project. Inside this file you can write the import for your Objective-C file e.g.:

#import "MyUIViewController.h"

This will import them to the project, so that they are compatible with the other Swift files. Then in your swift class you can refer to that Objective-C view controller like you would any other swift class, e.g.:

let myUIViewController = MyUIViewController()
Sign up to request clarification or add additional context in comments.

Comments

0

Yes it will work

I also work with my previous project and i imported my old Objective-C code into the Swift project making Bridge header for accessing that Objective-C UIViewController from Swift class. You directly assign your Objective-C UiViewcontroller class in the StoryBoard UIviewController and no need to rewrite any coad again for UIViewController.if you want to access Objective-C class inside your Swift Controller Class you need to make first Bridge header file then do this.

enter image description here

 //In Swift Controller you can call your Objective-C Class 
 let myView = MyController() // This is Objective-C controller
 myView.updatemyProfile() // Can call function like Objective-C here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.