为什么我使用Swift Cocoa App和桥接头获得未声明类型的'PubNub'编译器错误?

Jim*_*ins 12 xcode cocoa cocoapods pubnub swift

我正在开始一个新的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
Run Code Online (Sandbox Code Playgroud)

在我自动生成的桥接头中,我将PubNub导入为:

#import <PubNub/PubNub.h>
Run Code Online (Sandbox Code Playgroud)

和我的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
}


}
Run Code Online (Sandbox Code Playgroud)

由于使用未声明类型的PubNub时编译器错误,项目无法构建.我已经检查了构建设置和Swift编译器 - 代码生成部分显示它指向目标的桥接头文件(自动填充).

使用Xcode 6.4和pods版本0.38.2

Swi*_*ect 15

导入外部框架时没有桥接头

直接来自Apple Developer Documentation:

您可以导入具有纯Objective-C代码库,纯Swift代码库或混合语言代码库的外部框架.[...]您可以使用以下语法将框架导入到不同目标中的任何Swift文件中:

import FrameworkName
Run Code Online (Sandbox Code Playgroud)

固定

添加import PubNub框架.

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var client:PubNub?
    // ...
}
Run Code Online (Sandbox Code Playgroud)

使用单个import,PubNub声明,在Xcode编辑器中自动完成,编译,链接,构建和运行.


一步一步的Swift框架教程

由于下面的许多评论意味着总是需要桥接标题,所以当使用外部框架时,错误地如此,正如目前的use_frameworks!指令一样,在Podfile这里找到一个纯粹的Swift解决方案.接下来是一个Xcode可以下载和体验的项目.

iOS开发人员库中明确记录,在概念中使用Swift与Cocoa和Objective-C,混合和匹配章节,同一项目中的Swift和Objective-C部分,导入外部框架:

无论框架是用单一语言编写还是包含来自两种语言的文件,导入外部框架的过程都是相同的.

Podfile

platform :ios, '8.0'
use_frameworks!

target 'SO-31642385' do
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end
Run Code Online (Sandbox Code Playgroud)

安装吊舱

] 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.
Run Code Online (Sandbox Code Playgroud)

导入框架

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
    }
    // ...
}
Run Code Online (Sandbox Code Playgroud)

►在GitHub上找到此解决方案以及有关Swift Recipes的其他详细信息.

  • 这是另一种**方式.如果正确使用`Frameworks`,则不需要"桥接标题".`Pubnub`**是一个框架**.它包含在`use_frameworks!`中.只需添加`import PubNub`并完全绕过桥接机制. (2认同)