为什么我得到一个未声明类型“PubNub”编译器错误与Swift Cocoa应用程序和桥接头? [英] Why am I getting an undeclared type 'PubNub' compiler error with Swift Cocoa App and bridging header?

查看:343
本文介绍了为什么我得到一个未声明类型“PubNub”编译器错误与Swift Cocoa应用程序和桥接头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始一个新的Cocoa Swift项目,通过CocoaPods将PubNub SDK与以下Podfile结合:

I am starting a new Cocoa Swift Project that is incorporating the PubNub SDK via CocoaPods with the following 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

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

In my auto-generated bridging header I have the import for PubNub as:

#import <PubNub/PubNub.h>

和我的AppDelegate.swift文件:

And my AppDelegate.swift file:

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。我检查了构建设置,Swift编译器 - 代码生成部分显示它指向目标的桥接头文件(自动填充)。

The project fails to build due to compiler errors on use of undeclared type PubNub. I've checked the build settings and the Swift Compiler - Code Generation section shows it's pointed to the bridging header file of the target (auto-populated).

使用Xcode 6.4和pods版本0.38.2

Using Xcode 6.4 and pods version 0.38.2

推荐答案

否导入外部框架时的桥接标头



直接从 Apple开发人员文档

No Bridging-Header when Importing External Frameworks

Straight from Apple Developer Documentation:


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

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





$ b b

修正



添加 import PubNub 框架。

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var client:PubNub?
    // ...
}

在Xcode编辑器中声明 import , PubNub 自动完成,编译,链接,构建和运行。

With a single import, PubNub is declared, auto-completes in Xcode editor, compiles, links, builds and runs.

因为以下许多评论意味着Bridging-Headers总是需要的,所以当使用外部框架时,如 use_frameworks!指令在 Podfile ,在这里找到一个纯Swift 解决方案。随后是您可以下载和体验的 Xcode 项目。

Since many comments below imply that Bridging-Headers are always required, wrongly so when using External Frameworks as is presently the case with the use_frameworks! directive in the Podfile, find here a pure Swift solution. It is followed by an Xcode project you can download and experience with.

明确记录在 iOS开发人员库,在概念中将使用Swift与Cocoa和Objective-C 一章混合搭配 >,导入外部框架

Unambiguously documented in the iOS Developer Library, in concept Using Swift with Cocoa and Objective-C, chapter Mix and Match, section Swift and Objective-C in the Same Project, paragraph Importing External Frameworks:


导入外部框架 / strong>框架是以单一语言编写还是包含两种语言的文件。

strong> Podfile

Podfile

platform :ios, '8.0'
use_frameworks!

target 'SO-31642385' do
pod 'PubNub', '~>4.0'
pod 'Alamofire', '~> 1.3'
end

安装Pod

Install the pods

] 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: [NSObject: AnyObject]?) -> Bool {

        self.client = PubNub()
        return true
    }
    // ...
}






要下载整个项目,请搜索 SO-31642385 in Swift Recipes。


To download the full project, search for SO-31642385 in Swift Recipes.

这篇关于为什么我得到一个未声明类型“PubNub”编译器错误与Swift Cocoa应用程序和桥接头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆