Swift 的 framework 怎么调用 OC 的 framework

Swiftzixun • 于 2015-11-17 04:05:08 +0800 • 最后由 hjoeson2016-05-13 02:14:07 +0800 8216 阅读

我现在想做一个Swift的动态库,需要调用一个OC库的代码 这要怎么做,好像framework不能搞那个桥接的头文件

回复: 12
  • iBcker 2015-11-17 04:25:42 +0800

    看里面的 MixandMatch Importing Objective-C into Swift

  • iBcker 2015-11-17 04:27:28 +0800

    Importing Code from Within the Same Framework Target

    If you’re writing a mixed-language framework, you may need to access your Objective-C code from Swift and your Swift code from Objective-C.

    Importing Objective-C into Swift

    To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.

    To import Objective-C code into Swift from the same framework

    Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to “Yes". In your umbrella header file, import every Objective-C header you want to expose to Swift. For example:

    ```

    import

    import

    import

    ``` Swift will see every header you expose publicly in your umbrella header. The contents of the Objective-C files in that framework will be available in any Swift file within that framework target automatically, without any import statements. Use your custom Objective-C code with the same Swift syntax you use with system classes.

    let myOtherCell = XYZCustomCell() myOtherCell.subtitle = "Another custom cell"

  • dugege 2015-11-17 04:44:34 +0800

    @iBcker 他应该是没选这个

    file

  • zixun 2015-11-17 05:11:16 +0800

    @dugege 这个我试过了 不行

  • dugege 2015-11-17 05:44:23 +0800

    @zixun 完美工作

    file

  • dugege 2015-11-17 05:45:12 +0800

    你没import头文件吧?

  • zixun 2015-11-17 07:23:42 +0800

    @dugege 我们说的不是同一回事。我调的不是OC的代码,是一个外部的framework。请看标题~ 比如,我有一个swift的动态链接库,里面要调用AFNetworking的代码

  • dugege 2015-11-17 07:59:10 +0800

    咦,好神奇,竟然被一楼歪楼后我也看错了 :satisfied:

  • dugege 2015-11-17 08:14:26 +0800

    好像一楼没有歪,试了下,原理一样,swift的framework调用OC framework的代码 file

  • zixun 2015-11-17 18:36:29 +0800

    @dugege 你这代码都在一个工程里,不是framework。我是指编译后的.framework文件。比如调用某些不开源的SDK

  • zixun 2015-11-17 18:36:52 +0800

    @dugege http://stackoverflow.com/questions/25248598/importing-commoncrypto-in-a-swift-framework

  • hjoeson 2016-05-13 02:14:07 +0800

    @zixun 请问这个问题你解决了没

  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,见 Emoji cheat sheet
  • @name 会链接到用户页面,并会通知他
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
Ctrl+Enter