如何在一个框架,使用C dylib从斯威夫特文件 [英] How to use a C dylib from a Swift file in a framework

查看:216
本文介绍了如何在一个框架,使用C dylib从斯威夫特文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看起一个框架,它使用 libz.dylib 使用斯威夫特,但是看起来没有办法从内部斯威夫特导入。我试图进口zlib的进口libz进行,它没有工作。的ZLib已链接到目标。

好像让我的雨燕code看到的zlib 类的唯一方法是导入必要的标头中的一个桥接报头,但框架目标可以'T有一个桥接报头,所以有使用dylib的方法吗?


解决方案

在这个答案说明你可以导入系统模块:<一href=\"http://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework\">Importing CommonCrypto在斯威夫特框架

TLDR


  1. 添加一个文件夹命名为的ZLib 来你的框架目录

  2. 添加 module.map 文件包含以下内容


模块的ZLib [系统] {
    头\"/Applications/X$c$c-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/zlib.h\"
    出口 *
}

您可能需要在路径更改为您的ZLIB头。该示例适用于X code 7 Beta版和埃尔卡皮坦。

<醇开始=3>
  • 当前生成的module.map文件添加到您的构建设置

  • 导入的ZLib在迅速文件,你就可以去

  • 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?

    解决方案

    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

    这篇关于如何在一个框架,使用C dylib从斯威夫特文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆