swift-oc 互相调用

1 阅读1分钟

swift 调用 oc

swift 调用 oc 需要桥接头:模块名-Bridging-Header.h

swift 模块中创建 oc 类时会提示创建桥接头,选择 Create Bridgeing Header

image.png

然后会被自动配置到 Build Settings → Objective-C Bridging Header

image.png

如果没有提示创建那么就手动创建

demo 模块的 swift 调用 demo 模块 oc

在桥接头中import你要调用的 oc 类
比如 #import "DemoOcUtil.h"

然后就可以在 swift 中调用 oc 了

demo 模块的 swift 调用 lib 模块 oc

需要把h 文件都设置为 public 然后在demo模块的桥接头中 引入#import "MyLib/MyLibOcUtil.h" image.png

oc 调用 swift

oc 调用 swift需要模块头 : 模块名-swift.h
一般 Install Generated Header 默认是 yes,自动生成模块头, 直接在 oc 中 import 即可。

image.png

demo 模块的 oc 调用 demo 模块 swift

直接在 oc 中 import 模块头即可

demo 模块的 oc 调用 lib 模块 swift

确保有模块头, oc 可以用 @import MyLib (spm 模式,在m文件导入) 或者 #import "MyLib/MyLib-Swift.h" 导入

演示代码
github.com/m30102/OcSw…