how to use Objective-C project in my Swift project - objective-c

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.

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:
Now build your project once (⌘+B). You may get an error like this:
Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot)
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:
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:
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.
Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:
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.

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
and import statements.
Now, there is function/method in an objective C file getValue
which is used/accessed in swift project/file.
Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.

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.

Related

How to recognize touch inside SVG object in Swift?

I'm currently building an app where I need to recognize a touch point inside a SVG map. So I have a map with multiple rectangles and when the user touches one of these rectangles an action needs to be triggered. How can I solve this?
One option would be to mathematically calculate if the touch location lies in between a rectangle, but I am not a math genius.
My preferred option would be to use a framework. I found the SVGKit framework but unfortunately its written in Objective C.
You can use SVGKit for a Swift project. You have to use an umbrella header file, it is the 'master' header file for a framework.
Importing Objective-C into Swift
Access classes and other declarations from your Objective-C code in
Swift.
Overview
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.
The process for using your Objective-C declarations from your Swift
code within mixed-language targets differs slightly depending on
whether you’re writing an app or a framework. Both processes are
described below.
Import Code Within an App Target
To import a set of Objective-C files into Swift code within the same
app target, you rely on an Objective-C bridging header file to expose
those files to Swift. Xcode offers to create this header when you add
a Swift file to an existing Objective-C app, or an Objective-C file to
an existing Swift app.
If you accept, Xcode creates the bridging header file along with the
file you were creating, and names it by using your product module name
followed by "-Bridging-Header.h". Alternatively, you can create a
bridging header yourself by choosing File > New > File >
[operating system] > Source > Header File.
Edit the bridging header to expose your Objective-C code to your Swift
code:
In your Objective-C bridging header, import every Objective-C header you want to expose to Swift.
In Build Settings, in Swift Compiler - Code Generation, make sure the Objective-C Bridging Header build setting has a path to the
bridging header file. The path should be relative to your project,
similar to the way your Info.plist path is specified in Build
Settings. In most cases, you won't need to modify this setting.
Any public Objective-C headers listed in the bridging header are
visible to Swift. The Objective-C declarations are automatically
available from any Swift file within that target, with no import
statements. Use classes and other declarations from your custom
Objective-C code with the same Swift syntax you use for system
classes.
Import Code Within a Framework Target
To use the Objective-C declarations in files in the same framework
target as your Swift code, you’ll need to import those files into the
Objective-C umbrella header—the master header for your framework.
Import your Objective-C files by configuring the umbrella header:
Under Build Settings, in Packaging, make sure the Defines Module setting for the framework target is set to Yes.
In the umbrella header, import every Objective-C header you want to expose to Swift.
Swift sees every header you expose publicly in your umbrella header.
The contents of the Objective-C files in that framework are
automatically available from any Swift file within that framework
target, with no import statements. Use classes and other declarations
from your Objective-C code with the same Swift syntax you use for
system classes.
Source
Additional information
How to include Objective-C frameworks in your Swift project

Importing Swift into Objective-C: Objective-C header "-Swift.h" is not created

In my Swift Project, I added a new cocoa Objective-C class (UIViewController), Xcode prompted me whether I want it to automatically generate an Objective-C bridging header file.
I am a bit confused because the generated file is ProductModuleName-Bridging-Header.h while, as per Apple documentation, I was waiting to get a ProductModuleName-Swift.h instead (In order to import Swift into Objective-C).
The ProductModuleName-Bridging-Header.h is mandatory in order to be able to import Objective-C from Swift, and not the inverse.
Any clarification on this?
Thanks
The behavior you describe is exactly correct. You're adding Obj-C to your Swift project, therefore Xcode offers to create a -Bridging-Header.h file which allows that imported Obj-C to be used in your Swift code. This is described in the documentation you linked under "Importing Objective-C into Swift".
Unlike the bridging header, the -Swift.h file doesn't appear in your file hierachy, and is not something you see or edit. It's generated and managed entirely by Xcode during the build process. You simply import into .m files when needed.

How to force `Xcode-generated header` ... in a Swift Project (Call Swift from Obj-C)

Xcode does not generate this -Swift.h header file when the project is a Swift project. The documentation says:
When you import Swift code into Objective-C, you rely on an
Xcode-generated header file to expose those files to Objective-C. This
automatically generated file is an Objective-C header that declares
the Swift interfaces in your target. It can be thought of as an
umbrella header for your Swift code. The name of this header is your
product module name followed by adding "-Swift.h".
If I try to include the -Swift.h file anyway, it says file not found.
I've been able to generate this file before in an Objective-C project.
But I want to call Swift from Objective-C, in a Swift project.
How do I call Swift from Obj-C in a Swift project?
Thanks!
If the Objective-C file in which you import *-Swift.h is in the same Swift target that contains the Swift code you are trying to use in Objective-C, you should have no trouble importing it. To make sure you are using the correct name, check the Objective-C Generated Interface Header Name under Build Settings. If the target is a framework, you may need to import the -Swift.h header a little differently:
#import "YourFrameworkModuleName/YourFrameworkModuleName-Swift.h"
whereas in an application target you would just do
#import "YourAppModuleName-Swift.h"
This is something I found from my experience.

How do you prepare an Objective-C framework for Swift use?

I have an existing framework & bundle written in Objective-C and would like to access it in Swift.
How would I go about doing that?
Here's what I got:
1) Attempting to import the ObjC framework. Notice that I'm not successful.
Within the Objective-C framework project... attempting to make it usable for Swift.
I set the 'Defines Module' to YES:
I setup the bridge header within the ObjC framework app:
Yet I'm unable to access the framework. Apparently Xcode 6.1.1 doesn't convert the Objective-C to an importable Swift framework interface.
What am I missing?
Firstly, make sure your objective-c code is up to date
Then you add your objective-c code to your project, xcode will automatically ask you if you want to create a bridging header.
In your bridging header file, you just need to include your objective-c header files
example:
#import MyClass.h
save your header, and after that, you can test by trying to use your objective-c classes inside your swift code base

how to add/import objective-c files to a swift app

just beginning with swift (absolut newby) i want to use existing objective-c files, i.e. vfr by Julius Oklamcak.
In "Using Swift with Cocoa and Objective-C" stands:
"To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift. Xcode offers to create this header file when you add a Swift file to an existing Objective-C app, or an Objective-C file to an existing Swift app."
My silly question: what means "import"?
If i only use "File/Add Files to ...", the dialog "Would you like to configure an Objecive-C bridging header?" will not appear.
When i use "File/New/File/Cacao Class (???)", the dialog appears, but only once?!
Is where any way to import a bundle/set of files? A lot of them in vfr...
Google swift bridging header.
See the first result.
I need 21 more characters.