首页 > 解决方案 > 如何在swift项目中使用objective c框架类?

问题描述

当我开发同时具有 swift 和目标 c 文件的框架时。当我在 swift 项目中访问 swift 类时,它工作正常,但是当我尝试在 swift 中访问目标 c 类时,我无法访问,我也在 swift 项目中桥接目标 c 文件。任何人都可以帮我解决上述问题。

//swift project
import ObjCSwiftFramework

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let callingswiftfile = SwiftFile() //calling the swift file 
       let callingobjectivecfile = ObjectiveCFile()//**Use of unresolved identifier 'ObjectiveCFile'**

    }

}


//Bridge file
#ifndef ObjCSwift_project_Bridging_Header_h
#define ObjCSwift_project_Bridging_Header_h
#import "ObjCSwiftFramework/ObjCSwiftFramework.h"
#endif

我还添加了框架屏幕截图供您参考 在此处输入图像描述

标签: iosobjective-cframeworks

解决方案


要在 swift 中使用 Objective-c 文件,您需要使用 Bridging header,现在只需在其中导入类名,如下所示

桥接文件的名称类似于 AppName-Bidging-Header.h

在此处输入图像描述

创建桥接头

https://mycodetips.com/ios/manually-adding-swift-bridging-header-1290.html

添加和导入 obj-c 类后文件将如下所示

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "CardIO.h"

推荐阅读