Swift Framework: Use System Header in Objective-C code - objective-c

I'm creating a new swift framework, that contains a class that I implemented in Objective-C. This works fine, but it needs sqlite3.h. I managed to create a module map, so that I can use sqlite3.h in my Swift classes already. However when I try to use it in the Objective-C header class with #import sqlite3; I get the error "Module 'sqlite3' not found.
For reference, I created the module map for sqlite3 similar to "ifaddrs" in:
https://ind.ie/labs/blog/using-system-headers-in-swift
And again, it works fine in Swift classes, unfortunately not in the Objective-C ones. Any ideas?
Thanks a lot!

Related

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.
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.

Using Swift in Objective-C project

Although I have followed and tried everything from This Thread and read all of Apple's Guide of Swift-ObjC interoperability, I'm unable to recognize or use Swift fies in my project.
I have created a Swift file that declares/defines a class called TorusView that inherits from UIView. I've tried to gain access to this class in another class MenuView by importing the bridging header, importing the Swift class, importing the class with the syntax *-swift.h (which seems to now be *.swift.hin Xcode7.2). I've made all of the modifications to my target build settings recommended in that lengthy Stack question and a variety of others from google searches.
Nothing I've tried has allowed me to create a TorusView objective in my objective-C class.
You need to import a header file YourAppName-Swift.h, it contains all the public (and internal if same target) declared types in Swift.
first: Build Settings --> defines module --> YES.
second:Product Module Name -->YOUR project NAME.
last:improt "YOUR project NAME-Swift.h" in your Object-c file
like this:
enter image description here

Writing tests for swift classes with XCTest written in Objective c

I'm facing a problem trying to write a test for class written in Objective-c and gets injected with a class written in Swift.
Example in the Test file:
SomeSwiftClass *swiftVar = [SomeSwiftClass new];
SomeObjectiveCClass *objVar = [[SomeObjectiveCClass alloc] initWithSwiftClass:swiftVar]
But the complier doesn't recognize the Swift class, and it doesnt support importing the "Target-Swift.h" too..
How can I write a test for both Objective c and Swift at the same TestFile ?
There are quite a few things that need to be in place if you want to use a Swift class in Objective-C.
The bridging header is required "Target-Bridging-Header.h"
Precede the class definition with #objc in the .swift file.
If you're still stuck, try deleting your .swift files and adding them to the project again, and Xcode will ask to generate the bridging header for you.
Check out the documentation: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID138
Or this link:
Swift to Objective-C header not created in Xcode 6
I managed to get it working, so if you post more of your code or share what you're doing I might be able to help.

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

Importing sqlite3.h to a Swift project via a Obj-C bridging header doesn't work

I have created a file called BridgingHeader.h and it contains only one line:
#import <sqlite.h>
And of course the framework libsqlite3.dylib is imported either.
In my build settings I've also set the value of Objective-C Bridging Header to <ProjectName>/Bridging-Header.h
BUT…
When I add import sqlite3 to my Swift class it says that it cannot find the module sqlite3.
For anyone who does want to use SQLite 3 directly, see this Gist:
https://gist.github.com/zgchurch/3bac5443f6b3f407fe65
Thanks to #trojanfoe I am using now a library which is an Objective-C wrapper for the sqlite3 framework. (FMDB)
Got it to work by importing the ObjC header in my BridgingHeader.h file and using the class without importing anything in the Swift class.
Conclusion: Only Objective-C files can be imported into the bridging header…