Monday, January 19, 2015

How to use both Objective C and Swift in same Project


With the introduction of new language Swift, developers can use this language to develop the iOS project. But most of the APIs are available in Objective C. So if any developer is developing the project in Swift language and they wants to use these Objective C API in there project then they can do it.

Even Developers can create the project with mix and match of both languages Objective C and Swift.

This post will tell you how you can use both language in the application.


Use Objective C in Your Swift project-

If you want to use Swift file in your Objective C project, just create a Objective C file and Xcode will prompt you asking “Would you like to create Objective C Bridging Header?”





When you tap on Yes it will create a Bridging Header file with name "ProductName-Bridging-Header.h"

You can import your Objective C code to Swift by importing your header file in this Bridging header file.





Use Swift in Your Objective-C project-


In order to use Swift file in your Objective C project, just create a Swift file by selecting File> New > Swift.



Now Xcode will prompt you asking “Would you like to create Objective C Bridging Header?”
 You can import your Swift code to any Objective C file by using this syntax-

#import “ProjectName-Swift.h”



You can download the sample app from Github.



No comments:

Post a Comment