美文网首页
Swift5-swift和oc混编

Swift5-swift和oc混编

作者: Jesscia_Liu | 来源:发表于2020-05-19 10:20 被阅读0次

    Tips:swift项目和oc项目中swift和oc混编做法一样
    本文中项目名为Test,即Build Settings中Product Name配置为Test

    productName

    一、swift中调用oc类

    • 1.创建桥接文件 Test-Bridging-Header.h,检查Build Settings中Objective-C Beidging Header配置Test/Test-Bridging-Header.h
    • 2.在桥接文件Test-Bridging-Header.h中#import MyView.h
    • 3.直接在swift类中按照swift语法使用MyView即可
    //Test-Bridging-Header.h
    #import "MyViewh"
    
    import UIKit
    class TestViewController: UIViewController {
        func testOC(){
            let tmpV = MyView()
            tmpV.test1()
            self.view.addSubview(tmpV)
        }
    }
    

    二、oc中调用swift类

    • 1.在oc类中 #import "Test-Swift.h",检查Build Settings中Objective-C Generated Interface Header Name配置Test-Swift.h
    • 2.直接在oc类中使用oc语法调用swift类
    #import "MyView.h"
    #import "Test-Swift.h"  //ProductName-Swift.h
    
    @implementation MyView
    
    - (void)test1{
        testSwiftView *view = [[testSwiftView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        view.backgroundColor = [UIColor redColor];
        [self addSubview:view];
    }
    @end
    

    三、检查Build Settings配置

    Build Settings配置

    相关文章

      网友评论

          本文标题:Swift5-swift和oc混编

          本文链接:https://www.haomeiwen.com/subject/fpjpohtx.html