And*_*rew 29 xcode unit-testing ios swift
我在我的XCode项目中添加了一个新的测试目标.我的项目有Swift代码和Objective-C代码,并有一个桥接头.在将桥接头添加到UnitTesting的构建设置或执行操作时import MyTarget,我收到错误:
failed to import bridging header
我试过了:
defines modules到Yes.我也试过制作一个样本项目,它构建并且运行良好.把我所拥有的一切都带到一个干净的新项目中,这不是一个选择.
Vic*_*hoy 38
如果你使用Pod作为包管理器,必须设置搜索路径等.给出一个简单的方法,
尝试在Podfile中添加它
target 'YourProductTests' do
inherit! :search_paths
# Pods for testing
end
Run Code Online (Sandbox Code Playgroud)
和 pod install
这个对我有用.
如果上述解决方案不适合您.尝试手动设置
单击您的测试目标 - >构建设置 - >选项卡:全部和组合 - > Swift编译器 - 代码生成 - >目标C桥接标题:添加您的xxx-bridging-header
选中"搜索路径"中,设置了值Framework Search Path,Header Search Paths,Library Search Path根据您的主要目标.也许某些搜索路径会丢失,请再次手动添加.
这可能对某人有用:如果将单元测试目标添加到已包含 CocoaPods 和一些 Objective-C 库 pod 的现有项目中。
以下步骤解决了无法导入桥接头问题。
选择您的项目 -> 构建设置 -> 搜索“定义模块” -> 给出“是”
从“YourProduct”目标复制 Objective-C 桥接标头路径并将其粘贴到“YourProductTests”目标的桥接标头路径中。
...
target 'YourProduct' do
# Add Pods for your product here...
target 'YourProductTests' do
inherit! :search_paths
# Pods for product testing, if any
end
end
Run Code Online (Sandbox Code Playgroud)
然后从项目的根文件夹执行“ pod install ”。这将填充“YourProductTests”的标题搜索路径。
最后一步验证“YourProduct”和“YourProductTests”目标的标头搜索路径应该相似。
在这一点上,我从来不需要import MyTarget让单元测试在 Swift 中工作。
常见解决方案
$(SRCROOT)用于引用您的桥接头路径,请确保它被评估为正确的路径。.xcodeproj对您的工作项目之一进行文件差异,并匹配任何可能不同的相关值。桥接头系统并不完美,但这里有一些我遇到的问题。
@Victor Choy解决方案适用于我,但是我不得不像这样将测试目标移动到产品目标中:
target 'YourProduct' do
# Pods for product
target 'YourProductTests' do
inherit! :search_paths
# Pods for product testing
end
end
Run Code Online (Sandbox Code Playgroud)
这对我不起作用:
target 'YourProduct' do
# Pods for product
end
target 'YourProductTests' do
inherit! :search_paths
# Pods for product testing
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11034 次 |
| 最近记录: |