How to use a C dylib from a Swift file in a framework

前端 未结 2 1459
醉酒成梦
醉酒成梦 2021-02-06 11:49

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

2条回答
  •  暖寄归人
    2021-02-06 12:00

    You can import system modules as described in this answer: Importing CommonCrypto in a Swift framework

    TLDR

    1. Add a Folder named ZLib to your framework directory
    2. Add a module.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.

    1. Add the currently generated module.map file to your build settings
    2. Import ZLib in your swift file and you will be ready to go

提交回复
热议问题