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?

Adrian
  • 16,233
  • 18
  • 112
  • 180
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • 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. – Larme Jun 16 '16 at 13:10

2 Answers2

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()
Tometoyou
  • 7,792
  • 12
  • 62
  • 108
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
Community
  • 1
  • 1
Anand Nimje
  • 6,163
  • 4
  • 24
  • 43