在.framework 中,OC 和 Swift 互調(diào)

OC 和 Swift 互調(diào)(.framework 中和普通項目中)

Framework

實現(xiàn) OC 與 Swift 互調(diào),外部可訪問 OC 與 Swift 文件

前期準備:在 Framework 生成如圖幾個文件


image-20210727111105146.png

其中 SwiftTest.swift 和 OCTest.h 供外部調(diào)用

TARGETS->Build Phases->Headers->Public,拖入umbrella header,再把 SwiftTest.swift 和 OCTest.h 拖進來

image-20210727113959066.png

調(diào)用鏈:

  1. 外部 -> SwiftTest.swift->OCImportSwiftTest.h->SwiftImportOCTest.swift

  2. 外部->OCTest.h->SwiftImportOCTest.swift->OCImportSwiftTest.h

在.framework 中 Swift 調(diào)用 OC 方法

  1. 需要調(diào)用的 OC 類放到 umbrella header 里

  2. Framework TARGETS->Build Settings->Packaging->Defines Modules 設(shè)為 YES

#import <UIKit/UIKit.h>

//! Project version number for BaseBluetooth.
FOUNDATION_EXPORT double BaseBluetoothVersionNumber;

//! Project version string for BaseBluetooth.
FOUNDATION_EXPORT const unsigned char BaseBluetoothVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <BaseBluetooth/PublicHeader.h>

#import <BaseBluetooth/OCTest.h> // 外部要調(diào)用.framework 的類
#import <BaseBluetooth/OCImportSwiftTest.h> // Framework 中 Swift 要調(diào)用的 OC 類

設(shè)置后 Swift 可調(diào)用 OCImportSwiftTest.h 內(nèi)的方法

在.framework 中 OC 調(diào)用 Swift 方法

  1. 需要被調(diào)用的 Swift 方法加上 @objc,再加上 public

  2. 在需調(diào)用 Swift 的 OC 文件中引入 #import <ProductName/ProductModuleName-Swift.h>,要注意這里不是橋接文件

幾個文件的內(nèi)容:

SwiftImportOCTest.swift

public class SwiftImportOCTest: NSObject {
    
    @objc public static func importObjectiveCTest(){
        print("importObjectiveCTest")

        let oc = OCImportSwiftTest()
        oc.ocTest()
    }
    
     @objc public func swiftTest() {
        print("swift func")
    }
}

OCImportSwiftTest.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface OCImportSwiftTest : NSObject
- (void)importSwiftTest;
- (void)ocTest;
@end

NS_ASSUME_NONNULL_END

OCImportSwiftTest.m

#import "OCImportSwiftTest.h"
#import <BaseBluetooth/BaseBluetooth-Swift.h>

@implementation OCImportSwiftTest

- (void)ocTest {
    NSLog(@"oc func");
}

- (void)importSwiftTest {
    NSLog(@"importSwiftTest");
    SwiftImportOCTest *swift = [[SwiftImportOCTest alloc]init];
    [swift swiftTest];
}
@end

外部調(diào)用的 swift 和 oc 文件內(nèi)容

SwiftTest.swift

import Foundation

public class SwiftTest: NSObject {
    
    public static func test() {
        print("swift test")
        let oc = OCImportSwiftTest()
        oc.importSwiftTest()
    }
}

OCTest.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface OCTest : NSObject
+(void)test;
@end

NS_ASSUME_NONNULL_END

OCTest.m

#import "OCTest.h"
#import <BaseBluetooth/BaseBluetooth-Swift.h>

@implementation OCTest
+(void)test {
    NSLog(@"OC test");
    [SwiftImportOCTest importObjectiveCTest];
}
@end

外部調(diào)用


import UIKit
import BaseBluetooth // Framework

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        testBtn.addTarget(self, action: #selector(testTUI), for: .touchUpInside)
    }
    
    lazy var testBtn: UIButton = {
        let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 60))
        btn.backgroundColor = .red
        view.addSubview(btn)
        return btn
    }()
    
    @objc func testTUI(){
        SwiftTest.test()
        print("============")
        OCTest.test()
    }
}

輸出

image-20210727143341871.png

可看到調(diào)用鏈

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容