Exinferis Exinferis - 5 months ago 32
Swift Question

Access App delegate from Objective C Project in Swift

We have an Objective C Project which is about to get some new features. These new features will be implemented in Swift. So far we have it all working with Swift and Objective C side by side and most of the things are quite pleasant to implement.

One thing I currently did not manage to accomplish is to access the (objc) app delegate from within swift, especially it's methods and the managed object context.

I tried

let appDelegate = UIApplication.sharedApplication().delegate! as UIApplicationDelegate


without any success.

How do I access the application delegate written in Objective C from my swift code and use it's methods as well as the managed object context?

Answer

Use your appdelegate class name instead of UIApplicationDelegate

let appDelegate = UIApplication.sharedApplication().delegate! as <<Your Appdelegate Class>>

and also import that appdelegate in Bridging-Header file. "Bridging-Header" is used for both side bridge connection