Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Objective-C project in my Swift project

Note: I know How to call Objective-C code from Swift, but I don't know below,

I want to use this EsptouchForIOS's Demo in my project. The demo is write in OC, it has a storyboard and controller. I want to know how to integrate the demo in my swift project, and use that storyboard and it's controller in my swift project.

like image 908
LF00 Avatar asked Aug 04 '17 08:08

LF00


People also ask

Can I use Objective-C in Swift?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

How do I open an Objective-C project in Xcode?

Click on the Xcode icon in the dock to launch the tool. Click Next and on the resulting options panel name the project sampleApp and select Foundation from the Type menu. Also verify that the Use Automatic Reference Counting option is selected.


3 Answers

I'll start writing from the very beginning. Suppose you have a project in Objective-C and now you want to continue your project's development in Swift. Follow the below guidelines: (This intends to your specific needs)

First choose to add a new file from File->New->File. In this process select your language as Swift. In the final step here, you will be prompted to Create Bridging Header. Select that:

Bridging Header creation

Now build your project once (+B). You may get an error like this:

iOS SDK version error message

Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot) Changing deployment target

To use Objective-C resources in Swift files:

Now that you've got one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you just include the header file of that class in this bridging header file. Like in this project, you have ESP_NetUtil and ESPViewController class and their header files too. You want to expose them to Swift and use them later in Swift code. So import them in this bridging header file:

Importing Objective-C header files in Bridging Header

Build once again. Now you can go to your Swift file. And use the Objective-C classes as like you use any resource in swift. See:

Objective-C classes used in Swift

N.B: You must expose all the class headers (that you're intending to use later in Swift) in that bridging header file

To use Swift resources in Objective-C files:

Now you may wonder, I've successfully used Objective-C resources in Swift. What about the opposite? Yes! You can do the opposite too. Find your Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name. This is the header file you will be using inside your Objective-C classes for any Swift to Objective-C interoperability. To know more check here.

Objective-C Generated Interface Header Name

Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:

Import and using Swift resource in Objective-C

You will get more understanding from the official apple documentation.

You can checkout the worked out version of your linked project here with Objective-C-Swift interoperability.

like image 105
nayem Avatar answered Nov 05 '22 21:11

nayem


So according to your question, you have added an objective C bridge in your swift project using How to call Objective-C code from Swift.

Now, import all headers (.h) files of your objective-c source code (demo project) that you want to direct use in swift file.

For example, your demo project has EsptouchForIOS following header (file with extension .h) files in project source code.

ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h

import a header file in your bridge, which you want to use in your swift code. Suppose in your swift code you want touch delegate ESPTouchDelegate then write,

#import "ESPTouchDelegate.h"

Here is snapshot of your demo integration in my Test Swift project with bridge

enter image description here

and import statements.

enter image description here

Now, there is function/method in an objective C file getValue

enter image description here

which is used/accessed in swift project/file.

enter image description here

Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.

like image 37
Krunal Avatar answered Nov 05 '22 19:11

Krunal


I have never tried to use objective-c from swift project. But I normally used swift classes from my objective-c project. I usually follow this instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from apple developer website.

like image 23
Nyein Ei Ei Tun Avatar answered Nov 05 '22 19:11

Nyein Ei Ei Tun