加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从Swift测试文件中调用Objective-C类

发布时间:2020-12-14 04:41:35 所属栏目:百科 来源:网络整理
导读:我正在努力实施单元测试.原始项目是用Objective-C编写的. 我创建了一个用Swift编写的新测试目标. 如何在我的测试文件中调用实际应用程序的Objective-C类? 我尝试过以下操作. @testable import MyModule 但是,这似乎只有在所有文件都在Swift中时才有效,而对
我正在努力实施单元测试.原始项目是用Objective-C编写的.

我创建了一个用Swift编写的新测试目标.

如何在我的测试文件中调用实际应用程序的Objective-C类?

我尝试过以下操作.

@testable import MyModule

但是,这似乎只有在所有文件都在Swift中时才有效,而对我来说并非如此.

我已经尝试了其他一些项目设置但是,这些似乎都不起作用.

我错过了一些明显的东西吗?

class MyTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        let vc = HomeViewController() //this line is failing. How do I expose this view controller written in objective c?
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }
}

解决方法

您需要创建桥接标头并将Objective-C文件添加到其中.
创建一个桥接头YourProductName-Bridging-Header.h,然后导入HomeViewControllerin桥接头.

To import Objective-C code into Swift from the same target

In your Objective-C bridging header file,import every Objective-C
header you want to expose to Swift. For example:

import “XYZCustomCell.h”

import “XYZCustomView.h”

import “XYZCustomViewController.h” In Build Settings,in Swift Compiler – Code Generation,make sure the Objective-C Bridging Header

build setting under has a path to the bridging header file. The path
should be relative to your project,similar to the way your Info.plist
path is specified in Build Settings. In most cases,you should not
need to modify this setting.

以下是一些可以帮助您的资源

> Using swift with Objective-C
> Answer to another stack overflow qustion
> How to use Objective-C in swift

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读