I need to create a swift wrapper for a C library for use on both iOS and macOS.
I have added the .a to the frameworks list and include it in library search path. I have added the header file to project and added it to User header search paths and I have added a module.modulemap to the project as well. Looking like this:
module codinglibc [system][extern_c] {
header “codinglibc.h”
export *
}
But when I import the module in Swift:
import Foundation
import codinglibc
I get this error message: No such module 'codinglibc'.
The project is a Cocoa Framework and I have been following this guide: https://medium.com/swift-and-ios-writing/using-a-c-library-inside-a-swift-framework-d041d7b701d9
I have looked at a lot of stackoverflow answers but most have been solved by adding import paths, which I already have done and Xcode can find both the header file and the static library so that is not the issue.
So:
1. Have I done something obviously wrong which I have missed?
2. Should I use briding headers instead?
Edit: I tried enabling Allow Non-modular Includes In Framework Modules
still no success
The answer is pretty trivial, yet annoying.
If you add the module.modulemap in an Xcode project, Xcode will not register it as "to be imported", so what you need to do is to add the path to you module.modulemap file in the header includes.
Related
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.
I need to create a framework in swift, that will internally use a static library. It would be great if this library was only visible internally in this framework. I have added all the files of this library to the framework, and I have created a module.modulemap file. This file looks like this:
framework module MySDK {
umbrella header "MySDK.h"
export *
}
module BluFi {
header "/Users/homedudycz/Documents/Developer/MyApp/MySDK/blufiprotocol/DH_AES.h"
export *
}
And I can now use objects from this static library inside my framework, but there is a problem. When I try to import my framework in some app I get an error: Missing required module 'BluFi'.
What am I doing wrong? Is there some other way to import this static library inside my framework internally?
I have also created a sample project with libraries that I need added in this SDK. Please take a look, and please update path in module.modulemap - I don't know how to make it relative to SDK path :/
https://www.dropbox.com/s/rnh7y7zr63zillv/TestApp.zip?dl=1
This question is a continuation of a previous one I'm currently migrating this (https://github.com/emilwojtaszek/leveldb-swift) library from swift 2 to swift 3/4. Here is the link to my fork https://github.com/lu4/leveldb-swift/tree/MigrationFromSwift2ToSwift3 (please note that the target branch is MigrationFromSwift2ToSwift3)
I was able to resolve (with many thanks to #Ruslan Serebriakov) all of the issues with initial code base and check that the code is running.
However after trying to update LevelDB C code to latest master I got new type of error which I don't understand how to resolve:
Include of non-modular header inside framework module 'LevelDB.c': '/Path/to/Project/leveldb-swift-migration/vendor/leveldb/include/leveldb/export.h'
I've did some research on the internet but the issues described there seem non-related with one I've stumbled on. Here is an image of the issue
Any help is appreciated, thank you in advance!
I'm never 100% certain with mixed language frameworks. But an error like this happens in Swift projects when:
since you cannot use a Bridging Header in frameworks,
you #import a C header in the Foo-Framework.h to expose it so the Swift code, and
the header is not itself marked "Public" to the target.
"Non-modular" seems to indicate "not part of the published module interface". At least with Swift--C mixes, you can only combine both through making the C headers public; no way to import private header files there, which is weird.
Give it a shot: Since you are obviously importing the file in non-Swift code, try to locate the export.h header file in your Xcode project, open the File inspector (⌘⌥1), and ensure public visibility in the framework target:
This issue is because the SDK u are importing is not modular or u can say modulemap file is missing. So make sure modulemap file should be available inside the framework folder. Also make sure that all public headers are listed explicitly in the modulemap. This issue will be resolve 100% if module map file will be include in the third party framework.
So I am following this tutorial https://www.youtube.com/watch?v=Q6qcrO8uNzU&feature=youtu.be for Parse and when defining a PFQuery variable I get this error: "Use of unresolved identifier 'PFQuery'".
Code Below:
var getMessages = PFQuery(className:"DevelopmentMessages")
How would this be fixed? Thanks in Advance! ;)
You haven't included the Parse framework in your project correctly. This is done about 7 minutes in during Part 1 of that tutorial: http://www.youtube.com/watch?v=Q6kTw_cK3zY
It is also covered in Parse's QuickStart instructions:
https://parse.com/apps/quickstart#parse_data/mobile/ios/native/existing
All you need to do is the "Install the SDK" portion but you also need to include the Parse framework's header in your Objective-C bridging header:
#import <Parse/Parse.h>
The problem might be because you might not have imported the libraries properly. Drag drop the library files into the project properly. Make sure you have added the library inside your project folder and not outside it. Sometimes it may also be the cause of the problem.
Note - Please don't forget to check the option "Copy files if needed" while dragging and dropping the library files.
Update - If you have named your project as "parse" then it will not work. So rename your project to any other name and try it.
I'm try to write a small game on IOS using socket and I've had a java socket server running with Flash client, but I got complier errors when I add the AlwaysRightInstitute/SwiftSockets source code to my swift project, and I did nothing after doing this
Here is the project structure:
the selected group "ARISockets" is the source code I drag into my project
and here are the errors(Use of unresolved identifier 'ari_fcntIVi'):
It seems that the errors cause by lack of some import file and I found "ari_fcntIVi" in ARISockets/UnixBridge.c,but I'm a really newer to Swift/Objective-C/C (I'm a AS3 developer), so that sucks me :/
I had the same problem with this library.
You need to create a Bridge file similar to "Import Objective-C into Swift" but this is C:
How to call Objective-C code from Swift
The issue was that you just copied over the sources instead of embedding the SwiftSockets framework. The ari_ prefixed functions used to required the bridging header of the SwiftSockets framework.
Having said that, the current SwiftSockets doesn't use bridging headers anymore, and you can directly embed the sources.