How to use an Objective-C project in my Swift project

Note. I know how to call Objective-C code from Swift , but below I don’t know

I want to use EsptouchForIOS Demo in my project. The demo is written in OC, has a storyboard and a controller. I want to know how to integrate the demo into my quick project, and use this storyboard and its controller in my quick project.

+9
objective-c xcode uiviewcontroller swift storyboard
source share
3 answers

I will start writing from the very beginning. Suppose you have a project in Objective-C, and now you want to continue developing your project in Swift. Follow the guidelines below: (This is according to your specific needs)

First select a new file from File-> New-> File . In this process, select your language as Swift . In the last step, you will be prompted for a Create Bridging Header . Choose this:

Header modification

Now create the project once ( ⌘ + B ). You may receive an error message:

IOS SDK Version Error Message

Change the target minimum deployment to a version supported by Swift . (Example in the following screenshot) Change Deployment Goal

Use Objective-C resources in Swift files:

You now have one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you simply include the header file of this class in this bridge header file. As in this project, you have the ESP_NetUtil and ESPViewController and their header files. You want to expose them to Swift and use them later in Swift code. Therefore, import them into this bridge header file:

Import <Objective-C header files into the header header

Build again. Now you can go to your Swift file. And use the Objective-C classes as if you were using any resource in swift. Cm:

Objective-C classes used in Swift

NB: you should expand all class headers (which you intend to use later in Swift) in that header file header

To use Swift resources in Objective-C files:

Now you may be surprised I successfully used Objective-C resources in Swift. How about the opposite? Yes! You can do the other way around. Find Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name . This is the header file that you will use in your Objective-C classes for any Swift to Objective-C functionality. Find out more here .

Objective-C Name of the generated interface header

Now in any of the Objective-C classes, import this interface header and use the Swift resources in the Objective-C code:

Import and use a Swift resource in Objective-C

You will get more insight from Apple's official documentation .

You can check out the developed version of the related project here with Objective-C - Swift .

+11
source share

So, according to your question, you added the C objective bridge to your quick project using How to call Objective-C code from Swift .

Now import all the header files (.h) of your Objective-C source code (demo project) that you want to use in the fast file.

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

ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h

import the header file into your bridge that you want to use in your fast code. Suppose in your fast code you want to push the ESPTouchDelegate delegate, then write

 #import "ESPTouchDelegate.h" 

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

enter image description here

and import operators.

enter image description here

Now in the getValue object file

there is a function / method

enter image description here

which is used / available in a quick project / file.

enter image description here

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

+2
source share

I have never tried using objective-c from a swift project. But I usually used the quick classes from my objective-c project. I usually follow these instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from the Apple developer website.

+2
source share

All Articles