Xcode Swift pods installation - import file not fo

2019-09-10 07:32发布

问题:

I'm working on a project in Xcode written in Swift. I'm using two pods, AFNetworking and BDBOAuth1Manager. They're both Obj-C libraries, so a simple bridging file to import them takes care of everything.

Now, the problem arrises when I try to include a third pod, SwiftyJSON, that's written in Swift. Here's what the Podfile looks like:

platform :ios, "8.0"
use_frameworks!

pod "AFNetworking"
pod "BDBOAuth1Manager"
pod "SwiftyJSON"

link_with 'TwitterSearch', 'TwitterSearch Tests'

After installing the above Podfile, the bridging header stops working, because it now it can't find the files I'm trying to import.

To clarifty, this is the bridging header file:

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

#import "BDBOAuth1RequestOperationManager.h"

It works when the Pods are just AFNetworking and BDBOAuth1Manager, which are written in Obj-C. It doesn't work when a third pod, SwiftyJSON, written in Swift, is included.

The exact error messages are:

  • Swift compiler error: "BDBOAuth1RequestOperationManager.h" file not found
  • Swift compiler error: Failed to import bridging header
    path-to-bridging-header

Any idea what this might be?

UPDATE: I've figured out why it wasn't working. When I manually added in SwiftyJSON, everything worked fine. The entire problem roots back to this line: use_frameworks! I'm not at all familiar with frameworks, but practically speaking you'd have to do the following:

#import "path/BDBOAuth1RequestOperationManager.h"

instead of

#import "BDBOAuth1RequestOperationManager.h"

回答1:

Workaround

Podfile:

platform :ios, "8.0"
use_frameworks!

target 'SwiftAndObjCPods' do
pod "SwiftyJSON"
end

target 'SwiftAndObjCPodsTests' do
pod "SwiftyJSON"
end

Project:

Drag & drop the current versions of AFNetworking and BDBOAuth1Manager directly into your project. You can do so by locating these in the Pods group > Pods > Show in Finder, and simply move these to a convenient tmporary location.

Bridging-Header.h

#import "AFNetworking.h"
#import "BDBOAuth1RequestOperationManager.h"

Swift usage

// No import
let a = BDBOAuth1RequestOperationManager(baseURL: baseURL, consumerKey: "consumer", consumerSecret: "secret")

Tested: links, builds, runs.



回答2:

No Workaround

Podfile

platform :ios, "8.0"
use_frameworks!

target 'ObjCSwiftPods' do
pod "AFNetworking"
pod "BDBOAuth1Manager"
pod "SwiftyJSON"
end

target 'ObjCSwiftPodsTests' do
pod "AFNetworking"
pod "BDBOAuth1Manager"
pod "SwiftyJSON"
end

-Bridging-Header

#import "../Pods/AFNetworking/AFNetworking/AFNetworking.h"
#import "../Pods/BDBOAuth1Manager/BDBOAuth1Manager/BDBOAuth1RequestOperationManager.h"

Note about Bridging-Header:

Replacing ../ by {full path to Pods}/ inexplicably got me there when Xcode acted-up.

Swift invocation

// necessary import
import SwiftyJSON

// test BDBOAuth1Manager
let url = NSURL(string: "http://ObjCSwiftPods.com")
let bdsm = BDBOAuth1RequestOperationManager(baseURL: url, consumerKey:"key", consumerSecret:"secret")

// test AFNetworking
let sec = AFSecurityPolicy()

// test SwiftyJSON
let json = JSON("{}")



回答3:

try adding version to the podfile like so

pod 'SwiftyJSON', '~> 2.2.1'