在我们iOS开发中,Swift和OC混编很是常见。我会用最简单的案例向大家展示效果,下面我们步入正题:
1、打开Xcode,创建Swift项目

2、新建ObjectiveC类,注意语言选择OC

3、然后会出现如下提示,选择 Create Bridging Header

3、创建 OC 类文件成功,这是会多出三个文件。前两个和以前一样,注意第三个 Swift-OC-Bridging-Header.h ,这个文件实质上是一个供 Swift 使用的头文件,在这里要把所有要暴露给 Swift 的 OC 头文件都添加进去,这样写 Swift 的时候就可以直接使用那些OC的类和方法啦。 Swift-OC-Bridging-Header.h 的组成是(项目名-Bridging-Header.h)

4、我们接着往下走。在 Swift-OC-Bridging-Header.h 中加入 "ObjectiveC.h"

5、在 ObjectiveC.h 中声明并实现一个对象方法


6、最后在 ViewController 中调用就可以了,然后编译并运行显示结果

态度决定一切
网友评论