本文章将从两个方向分别介绍 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 类 。

        

swift中 oc调用swift方法 oc调用swift库_ide

     1.2.2 建立桥接文件。 (很重要)

swift中 oc调用swift方法 oc调用swift库_ide_02

 

    一定要记得点击这个按钮。 

       1.2.3  接下来工程目录如下:

swift中 oc调用swift方法 oc调用swift库_swift中 oc调用swift方法_03

       

     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 遵循协议

  

swift中 oc调用swift方法 oc调用swift库_移动开发_04

     1.2.3.2 成为代理,并实现协议方法,更改controller.swift中hintLabel的text。



[objc]  view plain  copy

 


1. // push 到 oc controller  
2. @IBAction func pushAction(_ sender: AnyObject) {  
3. .init()  
4. .secondDelegate = self;  
5. self.navigationController?.pushViewController(secondVC,true)  
6. }  
7.   
8. // SecondViewControll的代理方法  
9. func refreshHintLabel(_ hintString: String!) {  
10. .text = "secondView textView.text = " + hintString;  
11. }

 

 

 

1.3.1 具体过程与1.2小节一样。 直接上代码。

 

        1.3.2 声明block;

         



[objc]  view plain  copy

 



1. typedef void(^RefreshHintLabelBlock)(NSString *hintString);  
2.   
3. @interface SecondViewController : UIViewController  
4. @property (nonatomic, copy) RefreshHintLabelBlock hintBlock;  
5. @end



 

 

1.3.3 block的回调。 SecondViewController.m中



[objc]  view plain  copy

 


1. #pragma mark 返回上一页回调 ,将用户输入的用户名传回给 ViewController.swift  
2. -(BOOL)navigationShouldPopOnBackButton{      
3. if (_hintBlock) {  
4. .text);  
5.     }  
6. return YES;  
7. }


 

 

        1.3.4 在swift类中调用 oc的block.

 



[objc]  view plain  copy

 


1. // push 到 oc controller  
2. @IBAction func pushAction(_ sender: AnyObject) {  
3. .init()  
4. .secondDelegate = self;  
5. .hintBlock = {(t:String?)in  
6. self.hintLabel.text = "secondView textView.text = " + t!  
7.     }  
8. self.navigationController?.pushViewController(secondVC,true)  
9. }

 

   工程已上传到git上,git地址: https://github.com/zhonggaorong/SwiftOrOc/tree/master

2.  OC工程中引入swift类。 具体实现过程。

    耽误了不少时间, 今天才开始写oc工程中引入swift类。

    demo地址: 

  

 



 



       2.1.1   新建一个基于OC语言的工程 ,取名 OcOrSwiftTwo



       2.1. 2  实现的功能为 : 从oc类 viewcontroller中, push 至 swift语言 SecondViewController  ,然后SecondViewController可以通过代理或者swift闭包把值传回viewcontroller. 



       2.1.3   当前文件目录看下图:  (第四个箭头: 桥接文件)



        



swift中 oc调用swift方法 oc调用swift库_ide_05



  



    2.2   如何在OC中实现swift的代理与闭包Block方法



            



    2.2.1 如何在oc中引入swift类。#import "工程名-swift.h"



[objc]  view plain  copy

 


1. #import "OcOrSwiftTwo-swift.h"


    注意: @objc(代理名)  才能在外部可见这个代理



[objc]  view plain  copy

 



    1. import UIKit  
    2. import Foundation  
    3.   
    4. // 必须加上@objc 代理才能在oc类中可见。  
    5. @objc(EditTextFieldDelegate)  
    6. protocol EditTextFieldDelegate:NSObjectProtocol {  
    7.  str: String) -> Void  
    8. }  
    9.   
    10. @objc(SecondViewController)  
    11. class SecondViewController: UIViewController {  
    12.   
    13.  editorDelegate:EditTextFieldDelegate?  
    14.  textField:UITextField?  
    15.  addButton:UIButton?  
    16.  pushButton:UIButton?  
    17.       
    18.  t:String) -> Void  
    19.  myEidtorBlock:editorBlock?  
    20.       
    21.     override func viewDidLoad() {  
    22. super.viewDidLoad()  
    23. self.view.backgroundColor = UIColor.white  
    24. .init(frame: CGRect.init(x: 50,60,200,50))  
    25. .placeholder = "输入返回首页的内容"  
    26. self.view.addSubview(textField!)  
    27.           
    28. .init(type: .custom)  
    29. .setTitleColor(UIColor.black,.normal)  
    30. .setTitle("pop",.normal)  
    31. .frame = CGRect.init(x: 50,150,200,50)  
    32. .layer.borderColor = UIColor.black.cgColor  
    33. .layer.borderWidth = 1.0  
    34. .addTarget(self,.touchUpInside)  
    35. self.view.addSubview(addButton!)  
    36.           
    37.           
    38.           
    39. .init(type: .custom)  
    40. .setTitleColor(UIColor.black,.normal)  
    41. .setTitle("push",.normal)  
    42. .frame = CGRect.init(x: 50,250,200,50)  
    43. .layer.borderColor = UIColor.black.cgColor  
    44. .layer.borderWidth = 1.0  
    45. .addTarget(self,.touchUpInside)  
    46. self.view.addSubview(pushButton!)  
    47.           
    48.     }  
    49.       
    50.     func popAction() -> Void {  
    51.           
    52. if editorDelegate != nil {  
    53. .editTextField((textField?.text)!)  
    54.         }  
    55.           
    56. if ((self.myEidtorBlock) != nil){  
    57. self.myEidtorBlock!((textField?.text!)!)  
    58.         }  
    59.           
    60. self.navigationController?.popViewController(animated: true)  
    61.     }  
    62.       
    63.       
    64.     func pushAction() -> Void {  
    65. .init()  
    66. self.navigationController?.pushViewController(three,true)  
    67.           
    68.     }



        2.2.3   在oc类中viewcontroller.m 文件中实现SecondviewController.swift的相关代理与闭包(block). 代码如下:



    [objc]  view plain  copy

     



    1. #import "ViewController.h"  
    2. #import "OcOrSwiftTwo-swift.h"  
    3.   
    4. @interface ViewController ()<EditTextFieldDelegate>  
    5. @property (nonatomic, strong) UITextField *showTextField;  
    6. @property (nonatomic, strong) UIButton *pushButton;  
    7.   
    8. @end  
    9.   
    10. @implementation ViewController  
    11.   
    12. - (void)viewDidLoad {  
    13. super viewDidLoad];  
    14. 50, 100 , 200, 50)];  
    15. .placeholder = @"swift传回的文本内容";  
    16. .adjustsFontSizeToFitWidth = YES;  
    17. .enabled = NO;  
    18. self.view addSubview:_showTextField];  
    19.       
    20.  buttonWithType:UIButtonTypeCustom];  
    21. .layer.CGColor];  
    22. .layer1.0];  
    23. 50, 200, 200, 50)];  
    24.  forState:UIControlStateNormal];  
    25. @"push" forState:UIControlStateNormal];  
    26. self@selector(pushAction) forControlEvents:UIControlEventTouchUpInside];  
    27.       
    28. self.view addSubview:_pushButton];  
    29. }  
    30.   
    31.   
    32.   
    33.   
    34. -(void)pushAction{  
    35. SecondViewController *second = [[SecondViewController alloc]init];  
    36. // second.editorDelegate = self;  
    37.       
    38. /* 
    39.       swift中的闭包回滴 
    40.      */  
    41. .myEidtorBlock = ^(NSString *str) {  
    42. .text = [NSString@"second传回信息: %@",str];  
    43.     };  
    44. self.navigationControllerYES];  
    45. }  
    46.   
    47. #pragma mark swift中的代理  
    48. -(void)editTextField:(NSString *)str{  
    49. .text = [NSString@"second传回信息: %@",str];  
    50. }  
    51.   
    52. - (void)didReceiveMemoryWarning {  
    53. super didReceiveMemoryWarning];  
    54. // Dispose of any resources that can be recreated.  
    55. }