cocoa pod:Swift编译器错误“无法导入桥接头”的原因?

时间:2016-11-11 12:35:56

标签: ios swift3 cocoapods xcode8

我正在使用cocoa pod 1.1.1版,swift 3.0.1和Xcode 8.1。我有一个应用程序,它使用像这样的可可豆荚(Podfile)

# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'
platform :ios, '8.0'
use_frameworks!

target 'TestApp' do
    pod 'GoogleAnalytics', '~> 3.14.0'
end

target 'TestAppTests' do
    pod 'Quick'
    pod 'Nimble'
end

我也有一些Objective-C文件,这就是我使用Bridging-Header.h文件的原因。

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

#import <CommonCrypto/CommonCrypto.h>

#import <GoogleAnalytics/GAI.h>
#import <GoogleAnalytics/GAIFields.h>
#import <GoogleAnalytics/GAIDictionaryBuilder.h>
#import <GoogleAnalytics/GAILogger.h>

#import <CoreBluetooth/CoreBluetooth.h>

#import "AModel+Level.h"
#import "AModel+AutoStatus.h"
#import "AModel+Settings.h"
#import "APacketData+Decoders.h"
#import "Reachability.h"

当我运行TestApp时,它运行得很好。但我运行单元测试用例,我在TestAppTests上出错 - &gt; Swift编译器错误 - &gt;无法在#import“GoogleAnalytics / GAI.h”上导入桥接标题“TestApp-Bridging-Header.h”。

我在podfile上使用这种技术解决了这个问题:

platform :ios, '8.0'
use_frameworks!

target 'TestApp' do
    pod 'GoogleAnalytics', '~> 3.14.0'
end

target 'TestAppTests' do
    pod 'GoogleAnalytics', '~> 3.14.0'
    pod 'Quick'
    pod 'Nimble'
end

当我将代码迁移到Swift 3.0.1时,我只想知道下面提到的几点:

1. Is it require to install every pods in different targets? or we have any alternate solution.
2. What is the best technique to handle this kind of problems?

请解释原因。

2 个答案:

答案 0 :(得分:1)

由于单元测试用例包含不同的目标,因此必须将cocoapods安装到该目标。所以你做的是正确的。

platform :ios, '8.0'
use_frameworks!

target 'TestApp' do
    pod 'GoogleAnalytics', '~> 3.14.0'
end

target 'TestAppTests' do
    pod 'GoogleAnalytics', '~> 3.14.0'
    pod 'Quick'
    pod 'Nimble'
end

<强> 1。是否需要在不同的目标中安装每个pod?或者我们有任何替代解决方案。

是的,您需要在所有不同的目标上安装pod。

<强> 2。处理这类问题的最佳技术是什么?

这是大多数人的做法之一。

但是对于更复杂的事情,如果你愿意,那就拿这个Reference

答案 1 :(得分:1)

将特定框架添加到测试目标对我有用。 在我运行单元测试的情况下,链接器找不到其中一个框架。

按照以下方式编辑我的Podfile之后,我就可以运行我的测试了:

target 'MyTarget' do

  pod 'somePod'
  pod 'somePod2'

  target 'MyTargetTests' do
      inherit! :search_paths

      pod 'somePod2'
  end
end
相关问题