将单元测试目标添加到 xcode - 无法导入桥接头不会消失

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28343493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:36:20  来源:igfitidea点击:

added unit testing target to xcode - failed to import bridging header won't go away

iosswiftxcodeunit-testingbridging-header

提问by Andrew

I added a new Test target to my XCode project. My project has Swift code and Objective-C code, and has a bridging header. Upon either adding the bridging header to UnitTesting's build settings, or doing import MyTarget, I'm getting the error:

我向我的 XCode 项目添加了一个新的测试目标。我的项目有 Swift 代码和 Objective-C 代码,并且有一个桥接头。在将桥接标头添加到 UnitTesting 的构建设置或执行时import MyTarget,我收到错误消息:

failed to import bridging header

failed to import bridging header

I've tried:

我试过了:

  • Adding the bridging header to project, unit testing and main target's build settings.
  • Changing defines modulesto Yes.
  • Moving the bridging header file to the root folder.
  • 将桥接头添加到项目、单元测试和主要目标的构建设置中。
  • 更改defines modulesYes
  • 将桥接头文件移动到根文件夹。

I've also tried making a sample project, which built and worked fine. Taking everything I have over into a clean new project isn't an option at this point.

我还尝试制作一个示例项目,该项目构建并运行良好。在这一点上,将我拥有的一切都转移到一个全新的项目中并不是一个选择。

回答by Victor Choy

If you use Pod as package manager, must set search path etc. Give a simple way,

如果你使用 Pod 作为包管理器,必须设置搜索路径等。给一个简单的方法,

try add this in Podfile

尝试在 Podfile 中添加这个

target 'YourProductTests' do
    inherit! :search_paths
    # Pods for testing
end

and pod install

pod install

It works for me.

这个对我有用。

If the above solution does not for you. Try set manually

如果上述解决方案不适合您。手动设置试试

  1. Click your Test target -> Build Setting-> tab: All & Combined -> Swift Compiler -Code Genration -> Objective C Bridging Header : add your xxx-bridging-header

  2. Check "Search Path", set up value of Framework Search Path, Header Search Paths, Library Search Pathaccording to your main target. Maybe some search path lose here, manually add again.

  1. 单击您的测试目标-> 构建设置-> 选项卡:全部和组合-> Swift 编译器-代码生成-> Objective C Bridging Header:添加您的 xxx-bridging-header

  2. 选中“搜索路径”中,设置了值Framework Search PathHeader Search PathsLibrary Search Path根据您的主要目标。可能这里丢失了一些搜索路径,重新手动添加。

回答by mkkrolik

@Victor Choy solution works for me, but I had to move test target inside product target like so:

@Victor Choy 解决方案对我有用,但我必须像这样将测试目标移动到产品目标中:

target 'YourProduct' do

   # Pods for product

   target 'YourProductTests' do
      inherit! :search_paths
      # Pods for product testing
   end
end

This did not work for me:

这对我不起作用:

target 'YourProduct' do
   # Pods for product
end

target 'YourProductTests' do
   inherit! :search_paths
   # Pods for product testing
end

回答by tfrank377

At this point, I've never had to import MyTargetto get unit tests to work in Swift.

在这一点上,我从来不需要import MyTarget让单元测试在 Swift 中工作。

Common Solutions

常见解决方案

  • I assume you tried it, but it wasn't clear if you added your bridging header to your app target and test target at the same time?
  • Another option, that may not be ideal, is to add a bridging header in your test target so that you actually have 2 bridging headers. They should look the same and would be a good test.
  • If using $(SRCROOT)to reference your bridging header path, ensure it is being evaluated to correct path.
  • If all else fails, you should do file diff of your .xcodeprojwith the one of your working project and match any relevant values that might be different.
  • 我假设您尝试过,但不清楚您是否同时将桥接头添加到应用程序目标和测试目标?
  • 另一种可能并不理想的选择是在您的测试目标中添加一个桥接头,以便您实际上有 2 个桥接头。他们应该看起来一样,这将是一个很好的测试。
  • 如果$(SRCROOT)用于引用您的桥接头路径,请确保它被评估为正确的路径。
  • 如果所有其他方法都失败了,您应该.xcodeproj对您的工作项目之一进行文件差异,并匹配任何可能不同的相关值。

The bridging header system isn't perfect, but here are a few issues I've run into.

桥接头系统并不完美,但这里有一些我遇到的问题。

回答by Ssrini

I faced the same problem. I did the following and the issue of 'Failed to import bridging header' is solved.

我遇到了同样的问题。我做了以下操作,解决了“无法导入桥接头”的问题。

Steps:

脚步:

  1. Select your project -> Build settings -> Search for 'Defines module' -> give 'YES'
  2. Copy Objective-C bridging header path
  3. Select your test target -> Build setting -> Swift compiler - General -> Objective-C bridging header -> Give Bridging header path.
  4. Make sure 'Header Search Paths' of test targets contains all the headers in ios Targets. Add them if any of the headers are missing.
  5. Build.
  1. 选择您的项目-> 构建设置-> 搜索“定义模块”-> 给出“是”
  2. 复制 Objective-C 桥接头路径
  3. 选择您的测试目标 -> 构建设置 -> Swift 编译器 - 常规 -> Objective-C 桥接头 -> 提供桥接头路径。
  4. 确保测试目标的“标题搜索路径”包含 ios 目标中的所有标题。如果缺少任何标题,请添加它们。
  5. 建造。

Reference: Refer this link.

参考:参考此链接。