本文章将从两个方向分别介绍 OC 与 swift 混编
1. 第一个方向从 swift工程 中引入 oc类
1. 1 如何在swift的类中使用oc类
1.2 如何在swift中实现oc的代理方法
1.3 如何在swift中实现oc的Block回调
2 二个方向从OC工程中引入swift类
2.1 如何在OC类中使用swift类
2.2 如何在OC中实现swift的代理方法
2.3 如何在OC中实现swift中类似Block回调
下面是具体的实现过程:
1.1 如何在swift的类中使用oc类?
1. swift工程中引入OC类。 具体实现过程。
1.1 新建一个swift工程类。 取名 swiftOrOC
1.2 实现的功能为 : 从swift. viewController.swift 中 push到 OC语言 secondViewController 控制器
1.2.1 新建SecondViewController 类 。
1.2.2 建立桥接文件。 (很重要)
一定要记得点击这个按钮。
1.2.3 接下来工程目录如下:
1.2.4 接下来就可以实现具体的跳转功能了。
ViewController.swift中具体实现
import UIKit class ViewController: UIViewController { @IBOutlet weak var hintLabel: UILabel! //稍后用来显示回调 // push 到 oc controller @IBAction func pushAction(_ sender: AnyObject) { let secondVC = SecondViewController.init() self.navigationController?.pushViewController(secondVC, animated: true) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
1.2 如何在swift中实现oc的代理方法
1.2.1 首先在 SecondViewController.h 中声明一个协议。具体代码
#import <UIKit/UIKit.h> @protocol SecondDelegate <NSObject> -(void)refreshHintLabel:(NSString *)hintString; @end @interface SecondViewController : UIViewController @property (nonatomic,weak)id<SecondDelegate> secondDelegate; @end
1.2.3 接下来就非常简单了,让ViewController.swift只需要成为SecondViewController的代理,然后遵循她的协议,就可以了。 具体代码如下。
1.2.3.1 遵循协议
1.2.3.2 成为代理,并实现协议方法,更改controller.swift中hintLabel的text。
- // push 到 oc controller
- @IBAction func pushAction(_ sender: AnyObject) {
- let secondVC = SecondViewController.init()
- secondVC.secondDelegate = self;
- self.navigationController?.pushViewController(secondVC, animated: true)
- }
- // SecondViewControll的代理方法
- func refreshHintLabel(_ hintString: String!) {
- hintLabel.text = "secondView textView.text = " + hintString;
- }
1.3 如何在swift中实现oc的Block回调
1.3.1 具体过程与1.2小节一样。 直接上代码。
1.3.2 声明block;