OC和Swift混编
2017-03-07 本文已影响0人
闻醉山清风
oc项目中添加swift代码,创建一个类,Language选择为swift,放到oc项目中:
创建文件
Bridging-header提示框
项目名-Bridging-Header.h
TTLInternetSensationViewController.swift
同样,在oc中调用swift的类,只需要在oc代码中导入一个(类名-Swift.h)的一个头文件就能使用,为了引用方便,我将其放入了全局的头文件中:
oc类中放入swift.h头文件
例举代码,如下
OC类中引入swift类

点击确认,x-code会自动弹出一个提示框,点击create按钮即可。

如此项目中会自动生成一个(项目名-Bridging-Header.h)的文件,在这个文件中引入对应的oc项目的头文件,即可在swift代码中调用oc类,例如
在项目名-Bridging-Header.h文件中导入

回到创建的swift类中(TTLInternetSensationViewController.swift),可以调用刚刚所引入的HomePageViewController类,这里用跳转页面作为代码示例:

同样,在oc中调用swift的类,只需要在oc代码中导入一个(类名-Swift.h)的一个头文件就能使用,为了引用方便,我将其放入了全局的头文件中:

例举代码,如下

以上