我正在启动一个新的 Cocoa Swift 项目,该项目通过 CocoaPods 将 PubNub SDK 与以下 Podfile 合并:
target 'myProject' do
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end
target 'myProjectTests' do
end
在我自动生成的桥接 header 中,我将 PubNub 导入为:
#import <PubNub/PubNub.h>
还有我的 AppDelegate.swift 文件:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var client:PubNub?
func applicationDidFinishLaunching(aNotification: NSNotification) {
let config = PNConfiguration( publishKey: "Your_Pub_Key", subscribeKey: "Your_Sub_Key")
client = PubNub.clientWithConfiguration(config)
client?.addListener(self)
client?.subscribeToChannels(["Your_Channel"], withPresence: false)
client?.publish("Swift + PubNub!", toChannel: "demo", compressed: false, withCompletion: nil) }
func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {
println(message)
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
由于使用未声明类型 PubNub 时出现编译器错误,项目无法构建。我检查了build设置,Swift 编译器 - 代码生成部分显示它指向目标的桥接头文件(自动填充)。
使用 Xcode 6.4 和 pod 版本 0.38.2
最佳答案
导入外部框架时没有桥接头
直接来自 Apple Developer Documentation :
You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. [...] You can import a framework into any Swift file within a different target using the following syntax:
import FrameworkName
修复
添加import PubNub
框架。
import UIKit
import PubNub
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var client:PubNub?
// ...
}
单import
, PubNub
在 Xcode 编辑器中声明、自动完成、编译、链接、构建和运行。
循序渐进的 Swift 框架教程
由于下面的许多评论暗示始终需要桥接 header ,因此在使用外部框架时是错误的,因为目前的情况是 use_frameworks!
Podfile
中的指令,在这里找到一个纯 Swift 解决方案。后面跟着一个 Xcode
您可以下载和体验的项目。
明确记录在 iOS Developer Library 中,在概念将 Swift 与 Cocoa 和 Objective-C 结合使用,混合和匹配一章,同一项目中的 Swift 和 Objective-C 部分,段落导入外部框架:
The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages.
播客文件
platform :ios, '8.0'
use_frameworks!
target 'SO-31642385' do
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end
安装 pod
] pod install
Downloading dependencies
Installing Alamofire (1.3.1)
Installing CocoaLumberjack (2.0.0)
Installing PubNub (4.0.4)
Generating Pods project
Integrating client project
Please close any current Xcode sessions and use `SO-31642385.xcworkspace` for this project from now on.
导入框架
import UIKit
import PubNub
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var client:PubNub?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.client = PubNub()
return true
}
// ...
}
► 在 GitHub 上找到此解决方案以及有关 Swift Recipes 的更多详细信息.
关于xcode - 为什么我在使用 Swift Cocoa App 和桥接 header 时会收到未声明的类型 'PubNub' 编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642385/