简体   繁体   English

不能在Objective-C单元测试中使用Swift类

[英]Can't use Swift classes inside Objective-C unit test

I have followed this recipe in order to use a Swift class in an existing Objective-C project MyProject . 我遵循这个配方,以便在现有的Objective-C项目MyProject使用Swift类。 That works fine. 这很好。

However, I'm still not able to use the same Swift class in the same project's unit tests. 但是,我仍然无法在同一项目的单元测试中使用相同的Swift类。 The compiler marks the line where my Objective-C unit test says #import "MyProjectTests-Swift.h" with file not found . 编译器标记我的Objective-C单元测试说明#import "MyProjectTests-Swift.h"file not found

If tried changing the test target's Product Module Name from its default MyProjectTests to MyProject as suggested in a comment to this (unanswered) question. 如果试图改变测试目标的Product Module Name从默认MyProjectTestsMyProject的建议中评论 (未回答)的问题。 However the compiler now marks the line where my Objective-C unit test says #import "MyProject-Swift.h" with file not found . 但是编译器现在标记了我的Objective-C单元测试说明#import "MyProject-Swift.h"file not found

So how can one integrate Swift classes into (XCTest) unit tests that are written in Objective-C? 那么如何将Swift类集成到用Objective-C编写的(XCTest)单元测试中呢? Does Apple provide any recipe? Apple提供任何食谱吗?

As per @SushiGrassJacob's comment (see here ) the following solves the issue: 根据@ SushiGrassJacob的评论(见这里 ),以下内容解决了这个问题:

  1. Make sure that Swift classes are targeted to both MyProject and MyProjectTest . 确保Swift类同时针对MyProjectMyProjectTest
  2. In unit test, #import "MyProjectTests-Swift.h". 在单元测试中,#import“MyProjectTests-Swift.h”。

Step-1 步骤1

Go to your test target > Build Settings > Header Search Paths, and add $CONFIGURATION_TEMP_DIR/YourProject.build/DerivedSources in it. 转到测试目标>构建设置>标题搜索路径,并在其中添加$CONFIGURATION_TEMP_DIR/YourProject.build/DerivedSources

Step-2 第2步

#import "YourProject-Swift.h"

Go to test target > Build Settings > Header Search Paths, and add $CONFIGURATION_TEMP_DIR/MyProject.build/DerivedSources in it. 转到测试目标>构建设置>标题搜索路径,并在其中添加$ CONFIGURATION_TEMP_DIR / MyProject.build / DerivedSources。 For me worked only without checking Swift classes targeted for MyProjectTests. 对我来说,只是在没有检查针对MyProjectTests的Swift类的情况下工作。 Source: https://medium.com/if-let-swift-programming/ios-tests-working-with-objective-c-and-swift-class-together-aaf40f91a27c 资料来源: https//medium.com/if-let-swift-programming/ios-tests-working-with-objective-c-and-swift-class-together-aaf40f91a27c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM