I'm looking at starting to use Swift in a framework, which uses libz.dylib
, but it looks like there's no way to import it from within Swift. I tried import zlib
and import libz
, which didn't work. ZLib is already linked to the target.
It seems like the only way to get my Swift code to see the zlib
classes is to import the necessary headers in a bridging header, but framework targets can't have a bridging header, so is there a way to use a dylib?
The Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.
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.
.dylib stands for dynamic library. A Static library (.a) A Static library (. a) is a pack of compiled classes, functions which can be used together with iOS app development project. It is a compiled binary or fat file and can be shared between projects.
I have added a C/C++ dylib library, with C Public API headers to Swift Project.
Assume that the library is, libModule.dylib.
Create a Directory with "Module"
mkdir Module; cd Module
Create a file module.map inside Module directory
Touch module.map
create a directory, this will be the header files directory, any name will serve.
mkdir Headers
copy all headers into Headers directory
cp -R path/Headers Headers/
open module.map & copy below contents to it, Swift team suggest, adding prefix C in front of library name, in case if its a C library.
module CModule {
umbrella "Headers" // for multiple files
header "filename.h" // for single header file, in this case even the Header directory is not needed.
export *
}
Go to Build Settings -> Swift Compiler - Search Paths
If step 9 is missed, we get famous @rpath/ Image not found crash.
Now Import module in swift as below:
import CModule // name given inside module.map
For system library, most steps don't require.. just create directory & module map file with correct header path and do 6, 7. Its seems step 10 not even required as its a system library.
module CommonCrypto [system] {
header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
export *
}
You can import system modules as described in this answer: Importing CommonCrypto in a Swift framework
TLDR
ZLib
to your framework directorymodule.map
file with the following contents
module ZLib [system] {
header "/Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/zlib.h"
export *
}
You might have to change the path to your zlib header. The example works for XCode 7 Beta and El Capitan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With