xcode 为什么我在使用 Swift Cocoa App 和桥接头时收到未声明类型的“PubNub”编译器错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31642385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:33:21  来源:igfitidea点击:

Why am I getting an undeclared type 'PubNub' compiler error with Swift Cocoa App and bridging header?

xcodeswiftcocoacocoapodspubnub

提问by Jim Hankins

I am starting a new Cocoa Swift Project that is incorporating the PubNub SDK via CocoaPods with the following Podfile:

我正在启动一个新的 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

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

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

#import <PubNub/PubNub.h>

And my AppDelegate.swift file:

还有我的 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
}


}

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).

由于使用未声明的类型 PubNub 时出现编译器错误,项目无法构建。我检查了构建设置,Swift Compiler - Code Generation 部分显示它指向目标的桥接头文件(自动填充)。

Using Xcode 6.4 and pods version 0.38.2

使用 Xcode 6.4 和 pods 版本 0.38.2

回答by SwiftArchitect

No Bridging-Header when Importing External Frameworks

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

Straight from Apple Developer Documentation:

直接来自Apple 开发者文档

You can import external frameworks that have a pure Objective-Ccodebase, a pure Swiftcodebase, or a mixed-languagecodebase. [...] You can import a frameworkinto any Swift file within a different target using the following syntax:

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

import FrameworkName


Fix

使固定

Add import PubNubframework.

添加import PubNub框架。

import UIKit
import PubNub

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

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

With a single import, PubNubis declared, auto-completesin Xcode editor, compiles, links, builds and runs.

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



Step-by-step Swift Framework Tutorial

Swift 框架分步教程

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 Swiftsolution. It is followed by an Xcodeproject you can download and experience with.

由于下面的许多评论暗示桥接头总是需要的,所以当使用外部框架时是错误的,就像目前在 中的use_frameworks!指令的情况一样,在Podfile这里找到一个纯 Swift解决方案。接下来是一个Xcode您可以下载并体验的项目。

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:

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

The process for importing an external framework is the samewhether the framework is written in a single language or contains files from both languages.

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

Podfile

播客文件

platform :ios, '8.0'
use_frameworks!

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

Install the pods

安装豆荚

] pod install

Downloading dependencies
Installing Alamofire (1.3.1)
Installing CocoaLumberHyman (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 Framework

导入框架

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
    }
    // ...
}


? Find this solution on GitHuband additional details on Swift Recipes.

? 在GitHub 上找到此解决方案,在Swift Recipes找到更多详细信息。