在OC项目中实现swift与oc混编 相互引用

 --------------------------------------------------------Begin--------------------------------------------------------------

  • oc引用swift
  1.      通过cocoaclass新建一个swift文件  
  2. finish->create,填好文件名 ,->finish,弹出是否要创建桥接文件的提示框
  3. 如果没有弹出是否创建桥接文件的提示框,则进入配置工程里看看此处是否为空,如果不是空的,就删掉,因为是否创建桥接文件只会弹出一次,若你已经选择了一次not create,则下次不会再提示,想要再次创建桥接文件需在这里将设置清空,再从1开始
  4.     建好后如图所示
  5.  更改工程里的配置
  6. 至此,文件创建好了,配置也做好啦,开始上代码,在需要引用swift的oc文件中导入头文件
    #import "test-Swift.h"。(上图中注意的名字(一般为工程名)-Swift.h)
    #import "ViewController.h"
    #import "test-Swift.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {

        TestSwift *ar = [[TestSwift alloc] init];

        [super viewDidLoad];

    }

     oc中引用swift完毕

  •          swift引用oc,如在TestSwift中要引用ViewController这个类
  1. 跟上面1-5步骤一样
  2. 在桥接文件里引入要引用的OC类的头文件,
    //
    //  Use this file to import your target's public headers that you would like to expose to Swift.
    //
    
    #include "ViewController.h"

     

  3. 在TestSwift文件里进行引用
    import UIKit
    
    class TestSwift: NSObject {
        let vc:ViewController = ViewController()
    }

     -----------------------------------------------------End--------------------------------------------------------------

 

posted @ 2017-02-08 17:17  culing1317  阅读(5798)  评论(0编辑  收藏  举报